In projects, we often need to get the absolute path to the project, which facilitates us to upload and download files. JS provides us with a method, although it is necessary to detour. The code is as follows:
function getRealPath(){ //Get the current URL, such as: http://localhost:8083/myproj/view/my.jsp var curWwwPath=window.document.location.href; //Get the directory after the host address, such as: myproj/view/my.jsp var pathName=window.document.location.pathname; var pos=curWwwPath.indexOf(pathName); //Get the host address, such as: http://localhost:8083 var localhostPaht=curWwwPath.substring(0,pos); //Get the project name with "/", such as: /myproj var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); //Get http://localhost:8083/myproj var realPath=localhostPaht+projectName; alert(realPath); }