您的位置:首页 > Web前端 > JavaScript

js获取项目根路径

2016-06-23 10:32 423 查看
//js获取项目根路径,如: http://localhost:8083/uimcardprj functiongetRootPath(){
//获取当前网址,如: 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;

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: