后端数据配置相对路径,前端添加网站根 URL (根路径)- js获取网站项目根路径- 获取根路径后的第一个斜杠前 / 的项目- - 判断url包含某字符串

1、js获取网站项目根路径

js获取项目根路径,如下:

http://localhost:8080/testproject/test.html

根路径:http://localhost:8080

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、获取根路径后的第一个斜杠前 / 的项目

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包含某字符串:

var path = window.document.location.pathname;
if (path=="/user/do/editpassword"){
    alert("ok");
}

参考链接

1. js获取网站项目根路径

你可能感兴趣的:(JavaScript,Vue,javascript,vue)