JS获取url参数,修改url参数

function getURL(){
var args = {};
var query = location.search.substring(1);  //获得了当前链接的中?号后的参数
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexOf(‘=‘);
if (pos == -1) continue;
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
value = decodeURIComponent(value);
args[argname] = value;
}
return args;
}

 

function getNewHref(index, value) {
var gets = getURL(), url = ‘?‘;

$.each(gets, function(key, val) {
if (index === key)
url += key + ‘=‘ + value + ‘&‘;
else
url += key + ‘=‘ + val + ‘&‘;
})

if (url === ‘?‘) {
return location.href + ‘?‘ + index + ‘=‘ + value;
}
else if (typeof gets[index] === ‘undefined‘) {
return location.href + ‘&‘ + index + ‘=‘ + value;
}
else {
return location.href.split(‘?‘)[0] + url.substr(0, url.length - 1);
}
}

 

function getDeleteHref(index1, index2) {
var gets = getURL(), url = ‘?‘;

$.each(gets, function(key, val) {
if (index1 !== key && index2 !== key)
url += key + ‘=‘ + val + ‘&‘;
})

return location.href.split(‘?‘)[0] + url.substr(0, url.length - 1);
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。