new URL(url).pathname ====兼容不了ie浏览器
// 使用window.location.href获取当前网页的URL
var url = window.location.href;
// 使用URL对象的pathname属性获取路径部分
var pathname = new URL(url).pathname;
//https://www.baidu.com/qcwap/login/index?id=123
//结果是 /qcwap/login/index
通用方法
获取当前相对路径的方法 /qcwap/login/index
function GetUrlRelativePath()
{
var url = document.location.toString();
var arrUrl = url.split("//");
var start = arrUrl[1].indexOf("/");
var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符
if(relUrl.indexOf("?") != -1){
relUrl = relUrl.split("?")[0];
}
return relUrl;
}
//获取网址域名的方法 返回 http://www.baidu.com
function gethostname(){
var url = document.location.toString();
var arrUrl = url.split("//");
var host=arrUrl[0]+"//"+arrUrl[1].split("/")[0]
return host
}