【问题标题】:A web partial update effect in Facebook with URL changed [duplicate]更改了 URL 的 Facebook 中的 Web 部分更新效果 [重复]
【发布时间】:2012-12-12 19:53:41
【问题描述】:

可能重复:
Modify the URL without reloading the page

我发现在 Facebook 中,如果我点击左侧部分的 NotesMusic 链接,只有中心部分会在 URL 更改时刷新,来自 www.facebook .com 到 www.facebook.com/Notes

据我所知,更改 URL 会触发整个页面重新加载。 Facebook 对其网页有什么魔力?

【问题讨论】:

  • 它叫做pushState。看看History.js。请注意,并非所有浏览器和版本都支持此功能,因此您应该考虑使用库而不是自行开发库,或者研究插件如何优雅地降级。
  • 它适用于 IE 7-8 吗?

标签: javascript html ajax page-refresh


【解决方案1】:

您可以使用 javascript 命令如 pushState/replaceState/popstate 来操作浏览器历史记录

例子:

function processAjaxData(response, urlPath){
     document.getElementById("content").innerHTML = response.html;
     document.title = response.pageTitle;
     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
 }

【讨论】:

    【解决方案2】:

    魔法是history.pushState()

    示例

    假设http://mozilla.org/foo.html 执行以下JavaScript:

    var stateObj = { foo: "bar" };
    history.pushState(stateObj, "page 2", "bar.html");
    

    这将导致 URL 栏显示 http://mozilla.org/bar.html,但不会导致 >浏览器加载 bar.html 甚至检查 bar.html 是否存在。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2015-07-14
    • 2018-11-05
    • 1970-01-01
    • 2017-04-05
    相关资源
    最近更新 更多