我们一步一步来,从传参开始
1. 传参
中文如果不作处理的话,会乱码哦。那就用encodeURI();
var url = 'https://www.***.com/goods.html?id=123'+'&name='+encodeURI('小钻石');
2. 接参
var postData = GetRequest();
var name = postData.name;
var id = postData.id;
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;
}
}
3. 结束🔚