//删除Url参数值 function DeleteUrlParam(url,name) { var reg = new RegExp("([&\?]?)" + name + "=[^&]+(&?)", "g") var newUrl = url.replace(reg, function (a, b, c) { if (c.length == 0) { return ''; } else { return b; } }); return newUrl; } //设置Url参数值 function SetUrlParam(url, pname, pvalue) { var strNewUrl = new String(); var strUrl = url; if (strUrl.indexOf("?") != -1) { strUrl = strUrl.substr(strUrl.indexOf("?") + 1); if (strUrl.toLowerCase().indexOf(pname.toLowerCase()) == -1) { strNewUrl = url + "&" + pname + "=" + pvalue; return strNewUrl; } else { var aParam = strUrl.split("&"); for (var i = 0; i < aParam.length; i++) { if (aParam[i].substr(0, aParam[i].indexOf("=")).toLowerCase() == pname.toLowerCase()) { aParam[i] = aParam[i].substr(0, aParam[i].indexOf("=")) + "=" + pvalue; } } strNewUrl = url.substr(0, url.indexOf("?") + 1) + aParam.join("&"); return strNewUrl; } } else { strUrl += "?" + pname + "=" + pvalue; return strUrl } }
参考网文: