【问题标题】:Javascript: How do I change the URL in the address bar without refreshing the page? [duplicate]Javascript:如何在不刷新页面的情况下更改地址栏中的 URL? [复制]
【发布时间】:2011-12-13 17:30:45
【问题描述】:

可能重复:
How do I, with JavaScript, change the URL in the browser without loading the new page?

我注意到像 GMail 和 GrooveShark 这样的网络应用程序可以在不刷新页面的情况下更改浏览器地址栏中的 URL。 Ajax 显然是用来改变内容的。如何更改网址?

【问题讨论】:

标签: javascript ajax web-applications


【解决方案1】:

Gmail 和 Grooveshark 只更改哈希,这是通过更改来完成的:

location.hash = 'blah'

如果你的目标是支持 HTML5 的浏览器,你可以使用 window.history.pushState 和 window.history.popState,见http://spoiledmilk.dk/blog/html5-changing-the-browser-url-without-refreshing-page

【讨论】:

    【解决方案2】:

    您可能想查看 HTML5 推送状态。我知道 iQuery Mobile 正在使用此功能在不触发页面加载的情况下修改位置 URL,它可能是适合您的解决方案。

    https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

    这是一个如何工作的例子:

    window.history.pushState(data, "Title", "/new-url");
    

    【讨论】:

    • 注意firefox不支持title参数(第二个),所以使用document.title = title。
    猜你喜欢
    • 2012-11-05
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多