获取window.location.href 的参数
时间: 2023-12-14 21:34:25 浏览: 83
showModalDialog和window.open
以下是获取window.location.href参数的两种方法:
1.使用正则表达式解析参数
```javascript
function getUrlParam(name, urlsearch) {
return (decodeURIComponent((new RegExp("[?|&]" + name + "=" + "([^&;]+(&|#|;|$)").exec(urlsearch || location.search) || [, ""])[1].replace(/\+/g, "%20")) || null);
}
// 示例
let id = getUrlParam("id", window.location.href);
console.log(id); // 输出:id参数的值
```
2.使用URLSearchParams对象解析参数
```javascript
let params = new URLSearchParams(window.location.search);
let id = params.get("id");
console.log(id); // 输出:id参数的值
```
阅读全文