function changeURLArg(url,arg,arg_val){ var pattern=arg+‘=([^&]*)‘; var replaceText=arg+‘=‘+arg_val; if(url.match(pattern)){ var tmp=‘/(‘+ arg+‘=)([^&]*)/gi‘; tmp=url.replace(eval(tmp),replaceText); return tmp; }else{ if(url.match(‘[\?]‘)){ return url+‘&‘+replaceText; }else{ return url+‘?‘+replaceText; } } } /** * 获取url里的参数 * @param arg 参数名 * @returns */ function getURLString(arg) { var reg = new RegExp("(^|&)" + arg + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }
//修改参数: var url = window.location.href; changeURLArg(url, "id", 99); // http://www.csdn.com/?id=99&ttt=3 changeURLArg(url, "haha", 33); // http://www.csdn.com/?id=99&ttt=3&haha=33 // 获取参数 var nameVal=GetQueryString("URL上的参数名");
原文:https://www.cnblogs.com/wtil/p/12658527.html