1.传参页面JavaScript代码:
window.location.href="test.jsp?username="+username;
2.接收参数页面:
window.onload=function(){
var user = GetRequest()
document.getElementById("userinfo").innerHTML = "欢迎:"+user.username+"同学";
document.getElementById("user").value=user.username;
}
function GetRequest() {
var url =decodeURI(decodeURI(location.search)); //获取url中"?"符后的字串,使用了两次decodeRUI解码
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
return theRequest;
}
}
本文介绍了一种使用JavaScript进行页面间参数传递的方法,并详细解释了如何在目标页面上解析URL中的参数。通过示例代码,展示了如何利用window.location.href进行传参,以及如何在接收页面使用自定义函数GetRequest()来读取并处理这些参数。
224

被折叠的 条评论
为什么被折叠?



