[英]how does Google append search parameters to url without reloading page?
[英]Append parameter in url without reloading page
我正在使用以下代碼將參數附加到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);
我想在不重新加載頁面的情況下向網址添加多個參數:
我想要網址:“ .... / search.php”
單擊txt2后,我想要url:“ .... / search.php#t_2”
單擊link2后,我想要url:“ .... / search.php#t_1&l_2”
您只能使用帶有HTML5功能的history.pushState(state, title, url)
來執行此操作。
有一個新功能旨在用更好的解決方案pushState
代替對location.hash
的使用。
window.history.pushState(data, "Title", "/new-url");
詳細信息: http : //badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.