window.location.href传递参数
时间: 2023-09-25 17:15:03 浏览: 128
window.open 以post方式传递参数示例代码
可以使用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 ]
阅读全文