history.pushState
現在のページの状態を履歴スタックにプッシュし、アドレス バーの URL を変更します。したがって、戻ると、その状態 (渡されたオブジェクト) が返されます。
現在、それがすべてです。新しいページの表示やページ タイトルの変更など、その他のページ アクションは、ユーザーが行う必要があります。
リンクする W3C 仕様は単なるドラフトであり、ブラウザによって実装が異なる場合があります。 たとえば、Firefoxtitle
はこのパラメーターを完全に無視します。
pushState
これは、私が自分のウェブサイトで使用している簡単な例です。
(function($){
// Use AJAX to load the page, and change the title
function loadPage(sel, p){
$(sel).load(p + ' #content', function(){
document.title = $('#pageData').data('title');
});
}
// When a link is clicked, use AJAX to load that page
// but use pushState to change the URL bar
$(document).on('click', 'a', function(e){
e.preventDefault();
history.pushState({page: this.href}, '', this.href);
loadPage('#frontPage', this.href);
});
// This event is triggered when you visit a page in the history
// like when yu push the "back" button
$(window).on('popstate', function(e){
loadPage('#frontPage', location.pathname);
console.log(e.originalEvent.state);
});
}(jQuery));