向地址栏中动态添加参数
function addParamsToUrl(url, key, val) { if(!val) { return url; } var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i"); var separator = url.indexOf('?') !== -1 ? "&" : "?"; if (url.match(re)) { return url.replace(re, '$1' + key + "=" + val + '$2'); } else { return url + separator + key + "=" + val; } }
用法: addParamsToUrl(window.location.href, 'test', '1')