1、js获取网站项目根路径
js获取项目根路径,如下:
原 http://localhost:8080/testproject/test.html
根路径:http://localhost:8080
javascript
function getRootPath(){
//获取当前网址,
// 如: http://localhost:8080/testproject/test.html
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录,如: testproject/test.html
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8080
var localhostPaht=curWwwPath.substring(0,pos);
return localhostPaht;
}
2、获取根路径后的第一个斜杠前 / 的项目
javascript
function getRootProjectPath(){
//获取当前网址,如: http://localhost:8080/testproject/test.html
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录,如: testproject/test.html
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8080
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名,如:/testproject
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
return projectName;
}
3、判断url包含某字符串:
javascript
var path = window.document.location.pathname;
if (path=="/user/do/editpassword"){
alert("ok");
}