简体   繁体   English

使用javascript / jquery单击DOM元素后,更改地址栏中的URL

[英]Change URL in address bar after click on DOM element with javascript/jquery

is there any way to change the URL in browser without reloading page after click a DOM element? 有什么方法可以在单击DOM元素后更改浏览器中的URL而无需重新加载页面?

    $('#filter_form').on('change', function () {
        $.ajax(options);
        //change the browser URL
    });

Ex: 例如:

Before: http://somepage.com/filters 之前: http://somepage.com/filtershttp://somepage.com/filters

After: http://somepage.com/filters?type=apartment 之后: http://somepage.com/filters?type=apartmenthttp://somepage.com/filters?type=apartment

Yes, this is possible with the history API . 是的,这可以通过history API实现 Both history.pushState and history.replaceState let you specify a new URL, eg: history.pushStatehistory.replaceState允许您指定一个新URL,例如:

history.pushState(null, null, "filters?type=apartment");

The URL is resolved in the usual way. 该URL以通常的方式解析。

Browser support is good , other than IE9 and earlier. 除了IE9和更早版本之外, 浏览器支持都很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM