js获取url中的参数对象、js生成带参数的url

时间:2024-04-04 19:33:15
 // 获取url中的参数,并返回一个对象
$.getRequestData = function() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = {};
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]] = decodeURIComponent(strs[i].split("=")[1]);
}
}
return theRequest;
}; // 生成一个带参数的url
$.createUrl = function(obj) {
var length = obj && obj.length,
idx = 0,
url = obj.url + '?';
for (var key in obj) {
if (key != 'url' && obj[key] !== null) {
url += (key + '=' + encodeURIComponent(obj[key]) + '&');
}
}
return url.substring(0, url.lastIndexOf('&'));
}