更新浏览器的 URL,无需重新加载页面
是否可以更改浏览器地址栏中显示的 URL,而无需让浏览器转到该页面?例如,在通过 AJAX 调用更新页面内容之后?
我的理解是这是不可能的,这就是为什么 twitter 和 facebook 等网站更新 ajax 调用上的哈希标签的原因。
直到今天,当我进入 http://8tracks.com/ 并开始使用右侧的过滤器时..打开和关闭不同的流派,我注意到即使它正在执行 ajax 调用来刷新页面上的内容,URL 也会动态更新。
有谁知道他们是如何做到这一点的?
(顺便说一句,我目前正在使用 Chrome,但是当我返回并使用 IE9 再次查看时,我注意到 URL 没有更新......这可能是 Chrome 独有的东西吗?)
Is it possible to change the URL shown in the browser's address bar without having the browser go to that page? Such as, for example, after updating a page's content via an AJAX call?
My understanding is that this is not possible, which is why sites such as twitter and facebook update the hash-tag on ajax calls.
That is until today, when I went on http://8tracks.com/ and started to play with the filter on the right hand side... turning different genres on and off, I noticed that even though it was doing ajax calls to refresh the content on the page, the URL was also being dynamically updated.
Does anyone know how they do this?
(aside, I'm currently using Chrome, but when I went back and looked again with IE9, I noticed that the URL was not being updated.. is this maybe a Chrome only thing?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在现代浏览器中,可以通过使用 HTML5 History API 实现这一点:
请参阅 https://developer.mozilla.org/en/DOM/Manipulated_the_browser_history#The_pushState%28% 29.c2.a0method
这适用于 Firefox、Chrome、Opera、Safari(不适用于 IE)。
This is possible in modern browsers by using the HTML5 History API:
See https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState%28%29.c2.a0method
This works in Firefox, Chrome, Opera, Safari (not IE).
您也可以使用 Jquery 历史记录插件。这也将支持 html4 浏览器。
这是一篇讨论它的文章: http://veerasundaravel.wordpress.com/2011/12/02/change-browser-url-with-reloading-the-page-jquery-html5/
这是另一个提供更多选项的问题: jQuery 历史记录插件
also you can use Jquery history plugin. This will give support to html4 browsers as well.
Here is an article talking about it: http://veerasundaravel.wordpress.com/2011/12/02/change-browser-url-with-reloading-the-page-jquery-html5/
here is another question that gives more options: jQuery History Plugin