问题描述
我正在使用以下代码将参数附加到 url.这工作正常,但是当参数附加到 url 时,页面正在重新加载.我想在不重新加载页面的情况下使用此功能.
I am using following code to append param to url. This is working fine but when parameter is appended in url, page is getting reloaded. I want to use this functionality without reloading the page .
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 添加多个参数:
}
I want to add multiple params to url without reloading the page like:
- txt1
- txt2
- txt3
- link1
- link2
- link3
我想要网址:..../search.php"
i want url : "..../search.php"
点击txt2后我想要网址:..../search.php#t_2"
after click on txt2i want url : "..../search.php#t_2"
点击link2后我想要网址:..../search.php#t_1&l_2"
after click on link2i want url : "..../search.php#t_1&l_2"
推荐答案
您只能使用 执行此操作history.pushState(state, title, url)
这是一个 HTML5 特性.
You can only do this using history.pushState(state, title, url)
which is an HTML5 feature.
这篇关于在 url 中附加参数而不重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!