<script type="text/javascript"> var url = window.location.href; url = changeURLArg(url, "key", "搜索值"), window.location.href = url; /* * url 目标url * arg 需要替换的参数名称 * arg_val 替换后的参数的值 * return url 参数替换后的url */ 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; } } return url + '\n' + arg + '\n' + arg_val; } </script>