Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问。
参数 | 含义 |
---|---|
hash | 设置或返回从井号 (#) 开始的 URL(锚)。 |
host | 设置或返回主机名和当前 URL 的端口号。 |
hostname | 设置或返回当前 URL 的主机名。 |
href | 设置或返回完整的 URL。 |
pathname | 设置或返回当前 URL 的路径部分。 |
port | 设置或返回当前 URL 的端口号。 |
protocol | 设置或返回当前 URL 的协议。 |
search | 设置或返回从问号 (?) 开始的 URL(查询部分)。 |
一 . 如何获取#号后的字符串
-
window.location.search:获取当前URL的'?'号开始的字符串
-
window.location.hash:获取当前URL的'#'后面的字符串
二 .js代码
//1.获取链接后的参数(不带#号)
getQueryString(name) {
   let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
let r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURIComponent(r[2]);
return null;
}
复制代码
//2.获取链接后的参数(带#号)
getQueryString(name) {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
if(window.location.hash.indexOf("?") < 0){
return null;
}
let r = window.location.hash.split("?")[1].match(reg);   
if (r != null) return decodeURIComponent(r[2]);
  return null;
}
复制代码
//3.使用
console.log('name is ',getQueryString('name'));
复制代码