【问题标题】:change url with jquery like facebook messages使用 jquery 更改 url,例如 facebook 消息
【发布时间】:2012-10-26 19:23:48
【问题描述】:

可能你认为这是重复的,但这不是重复的。

change-hash-without-reload-in-jquery 在本主题中,您在 # 字符后更改 url,但如果您查看 facebook 消息 url 更改,您可以看到 facebook 更改 url 没有任何额外的 # 字符。

facebook.com/message/user1

如果你点击第二条用户消息,它会变成这样:

facebook.com/message/user2

此 url 更改无需重定向和使用 # 字符。

【问题讨论】:

  • 这很可能是服务器端 URL 重写。
  • 它被称为HTML5 history API,这是一堆类似问题的重复。

标签: jquery facebook url


【解决方案1】:

History.js 是通过跨浏览器支持实现这一目标的最佳选择。您应该意识到旧版浏览器不支持直接操作 URL,而是使用井号 (#) 机制来代替它。

这是他们的入门部分的一部分:

(function(window,undefined){

    // Prepare
    var History = window.History; // Note: We are using a capital H instead of a lower h
    if ( !History.enabled ) {
         // History.js is disabled for this browser.
         // This is because we can optionally choose to support HTML4 browsers or not.
        return false;
    }

    // Bind to StateChange Event
    History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        History.log(State.data, State.title, State.url);
    });

    // Change our States
    History.pushState({state:1}, "State 1", "?state=1"); // logs {state:1}, "State 1", "?state=1"
    History.pushState({state:2}, "State 2", "?state=2"); // logs {state:2}, "State 2", "?state=2"
    History.replaceState({state:3}, "State 3", "?state=3"); // logs {state:3}, "State 3", "?state=3"
    History.pushState(null, null, "?state=4"); // logs {}, '', "?state=4"
    History.back(); // logs {state:3}, "State 3", "?state=3"
    History.back(); // logs {state:1}, "State 1", "?state=1"
    History.back(); // logs {}, "Home Page", "?"
    History.go(2); // logs {state:3}, "State 3", "?state=3"

})(window);

直接使用HTML5 pustState 查看这些示例:

类似主题的问题:

其他资源:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2011-11-16
    • 2013-12-26
    • 1970-01-01
    相关资源
    最近更新 更多