window.location.href 接收参数
时间: 2023-10-13 07:25:02 浏览: 69
Window.location.href 可以接收参数作为查询字符串(query string),这些参数可以通过在 URL 中添加问号和键值对来传递。例如:
```
http://example.com/?name=John&age=30
```
在此 URL 中,`name` 和 `age` 是参数名,它们的值分别为 `John` 和 `30`。可以使用 JavaScript 来获取这些参数的值,如下所示:
```javascript
// 获取 URL 中的参数
const params = new URLSearchParams(window.location.search);
const name = params.get('name');
const age = params.get('age');
console.log(name); // 输出 "John"
console.log(age); // 输出 "30"
```
在此示例中,`window.location.search` 返回查询字符串 `?name=John&age=30`,然后使用 `URLSearchParams` 对象将其解析为键值对集合。使用 `get` 方法来获取每个参数的值。
相关问题
window.location.href传递参数
可以使用window.location.href进行页面跳转并传递参数。例如,可以使用以下代码将参数传递给新页面:
window.location.href = "./punch/clock_frm.html?modFlag=" + modFlag + "&role=" + role;
在新页面接收参数并将其转换为可用的JSON格式,可以使用以下方法:
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") ! -1) {
var str = url.substr(1);
var strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
}
console.log(theRequest); //此时的theRequest就是我们需要的参数
}
如果在传递参数时遇到中文乱码问题,可以使用encodeURI对中文进行编码,如下所示:
window.location.href = 'aaa.html?Unit=' + encodeURI(encodeURI(中文内容));
在接收页面接收参数时,可以使用decodeURI解码回来,如下所示:
var param = decodeURI(window.location.href);<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [转载--window.location.href传参/传值汇总](https://blog.csdn.net/weixin_47382065/article/details/120264692)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [window.open 以post方式传递参数示例代码](https://download.csdn.net/download/weixin_38655011/13621948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
window.location.href携带参数
window.location.href可以用于页面跳转并携带参数。例如,可以使用以下代码将参数传递给另一个页面:
window.location.href = "./punch/clock_frm.html?modFlag=" + modFlag + "&role=" + role;
这样就可以将modFlag和role的值作为参数传递到clock_frm.html页面中。
另外,你还可以在新页面中接收参数并将其转换为可用的json格式。可以使用以下代码来实现:
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
var strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")] = decodeURIComponent(strs[i].split("="));
}
console.log(theRequest);
}
这样,theRequest对象中就包含了从参数中提取出的键值对。
总之,window.location.href可以用于页面跳转并携带参数,然后可以在目标页面中接收和处理这些参数。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [转载--window.location.href传参/传值汇总](https://blog.csdn.net/weixin_47382065/article/details/120264692)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [window.location.href传递js对象参数及获取参数](https://blog.csdn.net/qq_17152035/article/details/73692750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]