Dans les projets, nous devons souvent obtenir le chemin absolu vers le projet, ce qui nous facilite de télécharger et de télécharger des fichiers. JS nous fournit une méthode, bien qu'il soit nécessaire de se détourner. Le code est le suivant:
Fonction getRealPath () {// Obtenez l'URL actuelle, telle que: http: // localhost: 8083 / myproj / vue / my.jsp var curwwwpath = window.document.location.href; // Obtenez le répertoire après l'adresse de l'hôte, tel que: myproj / vue / my.jsp var pathname = window.Document.Location.PathName; var pos = curwwwpath.indexof (pathName); // Obtenez l'adresse hôte, telle que: http: // localhost: 8083 var localhostpaht = curwwwpath.substring (0, pos); // Obtenez le nom du projet avec "/", tel que: / myproj var projectName = pathname.substring (0, pathName.substr (1) .Indexof ('/') + 1); // Obtenez http: // localhost: 8083 / myproj var realPath = localhostpaht + projectName; alerte (realpath); }