html对URL传参数进行解析

跳转页面需要传参数到另外一个html页面,跳转链接可写一个js的function 

function doView(articleId) {

    window.location.href ="collectionArticleComment.jsp?articleId = 2576;
}
然而URL会被编码(encodeURL):
collectionArticleComment.jsp?articleId%20=%202576
因此我们先将URL解码(decodeURL)
进参数进行解析,用到如下方法:
function GetRequest() {
var url = location.search ;
url = decodeURI(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].replace(/\s/g,"")]=unescape(strs[i].split("=")[1].replace(/\s/g,""));
        }
}
return theRequest;
}
将返回一个对象
["articleId ":"2576"]





原文地址:https://www.cnblogs.com/zhangky/p/6848337.html