使用url传参,当A页面跳转至B页面时,将变量值拼接在url后面,B页面可以使用封装的函数获取url中拼接的变量值。例如:
A页面代码: window.location.href = B.html?uid=1000;
B页面代码: function GetRequest(){
var url = window.location.search; //获取url中"?"符后的字串
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]]=decodeURI(strs[i].split("=")[1]);
}
}
return theRequest;
}
//调用方式
var Request = new Object();
Request = GetRequest();
var uid = Request['uid']; //获取到了url中uid的值1000
另外一种方式,比上面的要好用一些
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
var r = window.location.search.substr(1).match(reg);
if (r!=null)
return (r[2]);
return null;
}
var uid= GetQueryString('uid');