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;
//获取当前网址,如: 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;
相关文章推荐
- JS刷新当前页面的几种方法总结
- js 弹出子页面与关闭子页面刷新父页面的问题
- json简介
- Extjs tree树的方法和配置项
- JS使用JSON作为参数实例分析
- JSR303中的来验证数据信息
- HTML5 Server-Sent Events with JSP example
- 【JavaScript】内置对象
- 【JavaScript】变量和基本包装类型
- JS判断当前浏览器来自PC或者手机
- JSP 弹出对话框的方式总结
- JavaScript 加载顺序和异步加载详解
- jseclipse 是eclipse插件,让你编写js代码感觉更爽
- javascript新窗口打开链接window.open()被阻拦的解决办法
- jackson json 转list/map
- Javascript 数组循环遍历之forEach
- Extjs中的迭代方法
- 关于js在html中执行顺序的问题
- js中遍历Map对象
- 键盘上回车按钮的js触发事件