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); // http://www.huistd.com/?id=99&ttt=3
///changeURLPar(test, 'haha', 33); // http://www.huistd.com/?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); // http://www.huistd.com/?id=99&ttt=3
///changeURLParm(test, 'haha', 33); // http://www.huistd.com/?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处理 的详细内容,更多请关注其它相关文章!
上一篇: python 多层for循环转递归/迭代
下一篇: JavaEye博客频道全新改版