我正在使用以下代码将参数 append 到 url。这工作正常,但是当在 url 中 append 参数时,页面正在重新加载。我想在不重新加载页面的情况下使用此功能。
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);
我想在不重新加载页面的情况下向 url 添加多个参数:
- txt1
- txt2
- txt3
- 链接1
- 链接2
- 链接3
我想要网址:“..../search.php”
点击txt2后 我想要网址:“..../search.php#t_2”
点击链接后 我想要网址:“..../search.php#t_1&l_2”
最佳答案
您只能使用 history.pushState(state, title, url)
执行此操作这是一项 HTML5 功能。
关于javascript - 在不重新加载页面的情况下在 url 中 append 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10445229/