0

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

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

facebook.com/message/user1

如果您单击第二条用户消息,它将更改为:

facebook.com/message/user2

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

4

1 回答 1

2

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 pushState查看这些示例:

类似主题的问题:

其他资源:

于 2012-11-06T21:53:54.520 回答