項目中經常使用的經過js獲取項目根路徑,代碼以下:javascript
//js獲取項目根路徑,如: http://localhost:8080/XXX function getRootPath(){ //獲取當前網址,如: http://localhost:8080/XXX/share/meun.jsp var curWwwPath=window.document.location.href; //獲取主機地址以後的目錄,如: XXX/share/meun.jsp var pathName=window.document.location.pathname; var pos=curWwwPath.indexOf(pathName); //獲取主機地址,如: http://localhost:8083 var localhostPaht=curWwwPath.substring(0,pos); //獲取帶"/"的項目名,如:/XXX var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); return (localhostPaht+projectName); }