js獲取服務器路徑

是一個方法,使用時直接調用即可

//獲取服務器路徑

function basePath() {
    //獲取當前網址,如: http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //獲取主機地址之後的目錄,如: /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //獲取主機地址,如: http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //獲取帶"/"的項目名,如:/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //獲取項目的basePath   http://localhost:8080/ems/
    var basePath = localhostPath + projectName + "/";
    return basePath;
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章