您可能认为这是重复的,但这并不重复。
change-hash-without-reload-in-jquery
在本主题中,您可以在#
字符后更改网址,但如果您查看Facebook消息网址更改,则可以看到facebook更改网址而没有任何额外的#
字符。
facebook.com/message/user1
如果您单击第二个用户消息,它将更改为:
facebook.com/message/user2
此网址更改时不会重定向并使用#字符。
答案 0 :(得分: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 pustState查看这些示例:
类似主题的问题:
其他资源: