js获取项目根路径

来源:互联网 发布:玛雅新域名 编辑:程序博客网 时间:2024/05/29 16:28

//js获取项目根路径,如: http://localhost:8083/uimcardprjfunctiongetRootPath(){    //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp    varcurWwwPath=window.document.location.href;    //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp    varpathName=window.document.location.pathname;    var pos=curWwwPath.indexOf(pathName);    //获取主机地址,如: http://localhost:8083    varlocalhostPaht=curWwwPath.substring(0,pos);    //获取带"/"的项目名,如:/uimcardprj    varprojectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);    return(localhostPaht+projectName);}


 

以上代码在本地测试通过,于服务器就出现问题,于是请采用下面方法:

 

 

 

/**

 *获取上下文 

 **/

function getWebRootPath() {    var webroot=document.location.href;    webroot=webroot.substring(webroot.indexOf('//')+2,webroot.length);    webroot=webroot.substring(webroot.indexOf('/')+1,webroot.length);    webroot=webroot.substring(0,webroot.indexOf('/'));    var rootpath="/"+webroot;    return rootpath;


 

0 0
原创粉丝点击