我正在使用以下代码将param附加到url.这工作正常但是当参数被添加到url中时,页面将被重新加载.我想在不重新加载页面的情况下使用此功能.
function insertParam(key, value)
{
key = escape(key); value = escape(value);
var kvp = document.location.search.substr(1).split('&');
var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');
if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
alert('sdfadsf');
break;
}
}
if(i<0) {kvp[kvp.length] = [key,value].join('=');}
//this will reload the page, it's likely better to store this until finished
document.location.search = kvp.join('&');
//alert(document.location.href);
Run Code Online (Sandbox Code Playgroud)
我想在不重新加载页面的情况下向url添加多个参数:
我想要网址:"..../search.php"
点击txt2后我想要网址:"..../search.php #t_2"
点击link2后我想要网址:"..../search.php#t_1&l_2"