本篇文章的内容是JS对URL的处理,现在在这里分享给大家,有感兴趣的朋友也可以参考一下本片的内容

///调用
/// var request = GetRequestParms();
/// request["参数"]
function GetRequestParms(name) {
var url = location.search; //获取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++) {
if (strs[i].split("=")[0].toLowerCase() == name) {
continue;
}
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
///追加或者更改Url参数
///例子changeURLPar(test, 'id', 99); // ?id=99&ttt=3
///changeURLPar(test, 'haha', 33); // ?id=99&ttt=3&haha=33


function changeURLPar(destiny, par, par_value) {
var pattern = par + '=([^&]*)';
var replaceText = par + '=' + par_value;
if (destiny.match(pattern)) {
var tmp = '/\\' + par + '=[^&]*/';
tmp = destiny.replace(eval(tmp), replaceText);
return (tmp);
}
else {
if (destiny.match('[\?]')) {
return destiny + '&' + replaceText;
}
else {
return destiny + '?' + replaceText;
}
}
return destiny + '\n' + par + '\n' + par_value;
}


///例子changeURLParm(test, 'id', 99); // ?id=99&ttt=3
///changeURLParm(test, 'haha', 33); // ?id=99&ttt=3&haha=33
function changeURLParm(url, ref, value) {
var str = "";
if (url.indexOf('?') != -1)
str = url.substr(url.indexOf('?') + 1);
else
return url + "?" + ref + "=" + value;
var returnurl = "";
var setparam = "";
var arr;
var modify = "0";
if (str.indexOf('&') != -1) {
arr = str.split('&');
for (i in arr) {
if (arr[i].split('=')[0] == ref) {
setparam = value;
modify = "1";
}
else {
setparam = arr[i].split('=')[1];
}
returnurl = returnurl + arr[i].split('=')[0] + "=" + setparam + "&";
}
returnurl = returnurl.substr(0, returnurl.length - 1);
if (modify == "0")
if (returnurl == str)
returnurl = returnurl + "&" + ref + "=" + value;
}
else {
if (str.indexOf('=') != -1) {
arr = str.split('=');
if (arr[0] == ref) {
setparam = value;
modify = "1";
}
else {
setparam = arr[1];
}
returnurl = arr[0] + "=" + setparam;
if (modify == "0")
if (returnurl == str)
returnurl = returnurl + "&" + ref + "=" + value;
}
else
returnurl = ref + "=" + value;
}
return url.substr(0, url.indexOf('?')) + "?" + returnurl;
}

相关推荐:

JS的表单传值与URL编码转换详解