Hash 模式
- 读取当前 URL Hash 中的查询参数
- 例如当前页面 url 为:
https://www.csdn.com/#/data?id=1
function getHashSearchParam(key) {
const search = /(?<=#.*\?).*/.exec(location.href)?.[0]
const usp = new URLSearchParams(search)
return usp.get(key)
}
getHashSearchParam("id") // "1"
Browser模式
- 读取当前 URL 中的查询参数
- 例如页面 url 为:
https://www.csdn.com?id=66#/aa/bb/123
function getSearchParam(key) {
return new URLSearchParams(location.search).get(key)
}
getSearchParam("id") // "66"