使用javascript更改URL链接而不刷新

the*_*hai 5 javascript browser-history

是否可以在不刷新页面的情况下自动将url example.com/4000/title-2/#!4000更改为example.com/4000/title-2?基本上是从URL中删除"/#!4000".请注意,在hashbang之前删除"/"并不仅仅是hashbang很重要.

Tob*_*ogh 10

不知道它是否足够你,它是否完全跨浏览器... chrome接受:

location.hash = "";
Run Code Online (Sandbox Code Playgroud)

但这会在地址栏中保留"#"

在完全支持html5历史API的现代浏览器中,您可以:

window.history.replaceState('Object', 'Title', '/4000/title-2');
Run Code Online (Sandbox Code Playgroud)

编辑:这不会改变浏览器的历史

编辑2:刚刚找到这个stackoverflow资源

  • [浏览器历史记录的 MDN 条目](https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history),和 [W3 条目`replaceState()`](http://www.w3.org/TR/html5 /history.html#dom-history-replacestate)。+1 表示很棒,我以前没有遇到过 =) (2认同)