首页 > 代码库 > JS获取url参数,修改url参数
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 = http://www.mamicode.com/pairs[i].substring(pos+1);
value = http://www.mamicode.com/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);
}
JS获取url参数,修改url参数