I know this has been asked many times before but answers were not descriptive enough to solve my problem. I don't want to change the whole URL of page. I want to append a parameter &item=brand
on button click without refresh.
Using document.location.search += '&item=brand';
makes the page refresh.
Using window.location.hash = "&item=brand";
append without refresh but with a hash #
which eliminates the usage/effect of the parameter. I tried to remove the hash after appending but that didn't work.
This answer和许多其他浏览器建议使用HTML5 History API,特别是history.pushState
方法,并且对于旧的浏览器,设置片段标识符以防止页面重新加载.但是怎么做呢?
Assuming the URL is: http://example.com/search.php?lang=en
如何使用HTML5 Push State方法或片段标识符追加&item=brand
,以便在不刷新页面的情况下输出如下所示:http://example.com/search.php?lang=en&item=brand
?
我希望有人能对如何使用HTML5 pushState将参数附加到现有/当前URL上有所帮助.或者,如何为同样的目的设置片段标识符.一个好的教程、演示或一段代码,再加上一点解释就好了.
到目前为止,我已经做了Demo件事.非常感谢您的回答.