js获取 url 参数的方法总结

方法1:

获取URL中的参数名及参数值的对象集合

/**
* [获取URL中的参数名及参数值的集合]
* 示例URL:"http://test/getIndex/indexRequest.html?uid=admin&passWord=123456&fid=1314&name=阿豪";
* @param {[string]} urlStr [当该参数不为空的时候,则解析该url中的参数集合]
* @return {[string]} [参数集合]

*@decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码。

*/
function GetRequest(url) {
var urlParams = location.search.substr(1);
var theRequest = new Object();
strs = urlParams .split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
return theRequest;
}

 

后期有时间补充正则的方法

posted @ 2021-11-25 15:11  浣熊sky  阅读(614)  评论(0编辑  收藏  举报