html获取url参数
取这个url的参数 ,原理是 window.location
hash 设置或返回从井号 (#) 开始的 URL(锚)。
host 设置或返回主机名和当前 URL 的端口号。
hostname 设置或返回当前 URL 的主机名。
href 设置或返回完整的 URL。
pathname 设置或返回当前 URL 的路径部分。
port 设置或返回当前 URL 的端口号。
protocol 设置或返回当前 URL 的协议。
search 设置或返回从问号 (?) 开始的 URL(查询部分)。
file:///E:/project_wangj07/kj190328_jingpaidanyemian/index.html?pid=1&type=1&typeId=1
function GetQueryString(name) {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
let r = window.location.search.substr(1).match(reg) // search,查询?后面的参数,并匹配正则
if (r != null) {
return unescape(r[2])
}
return null
}
console.log(GetQueryString('pid'))
console.log(GetQueryString('type'))
console.log(GetQueryString('typeId'))
效果