--- title: WindowEventHandlers.onpopstate slug: Web/API/WindowEventHandlers/onpopstate tags: - API - DOM - Event Handler - HTML DOM - HTML5 - Window - WindowEventHandlers - onpopstate - イベント - プロパティ translation_of: Web/API/WindowEventHandlers/onpopstate --- <div>{{APIRef}}</div> <p><span class="seoSummary">{{domxref("WindowEventHandlers")}} ミックスインの <strong><code>onpopstate</code></strong> プロパティは、ウィンドウの <code><a href="/ja/docs/Web/API/Window/popstate_event">popstate</a></code> イベントを処理するための {{event("Event_handlers", "event handler")}} です。</span></p> <p><code>popstate</code> イベントは、同じ文書の2つの履歴項目の間で、アクティブな履歴項目が変わるたびにウィンドウに発行されます。アクティブな履歴項目が <code>history.pushState()</code> を呼び出したことで作成されたり、 <code>history.replaceState()</code> を呼び出したことで影響されたりした場合、 <code>popstate</code> イベントの <code>state</code> プロパティが履歴項目の状態オブジェクトのコピーを保持します。</p> <div class="note"> <p><strong>メモ</strong>: <code>history.pushState()</code> 又は <code>history.replaceState()</code> を呼び出すことは、 <code>popstate</code> イベントのトリガーにはなりません。 <code>popstate</code> イベントは、戻るボタンをクリックしたり (又は JavaScript で <code>history.back()</code> を呼び出したり)、同じ文書で2つの履歴項目間を移動したりするように、ブラウザーのアクションを実行することのみがトリガーになります。</p> </div> <h2 id="Syntax" name="Syntax">構文</h2> <pre class="syntaxbox">window.onpopstate = <em>funcRef</em>; </pre> <ul> <li><code>funcRef</code> は、ハンドラ関数です。</li> </ul> <h2 id="The_popstate_event" name="The_popstate_event">例</h2> <p>例えば、<code>http://example.com/example.html</code> のページで次のコードを実行すると、示されているようにアラートを生成します。</p> <pre class="brush:js">window.onpopstate = function(event) { alert("location: " + document.location + ", state: " + JSON.stringify(event.state)); }; history.pushState({page: 1}, "title 1", "?page=1"); history.pushState({page: 2}, "title 2", "?page=2"); history.replaceState({page: 3}, "title 3", "?page=3"); history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}" history.back(); // alerts "location: http://example.com/example.html, state: null history.go(2); // alerts "location: http://example.com/example.html?page=3, state: {"page":3} </pre> <p>なお、元の履歴項目 (<code>http://example.com/example.html</code> の場合) には関連付けられる状態オブジェクトがありませんが、それでも2回目の <code>history.back()</code> の呼び出し後に項目がアクティブになったとき、 <code>popstate</code> イベントが発行されます。</p> <h2 id="Specifications" name="Specifications">仕様</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様</th> <th scope="col">状態</th> <th scope="col">コメント</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('HTML WHATWG', 'webappapis.html#handler-window-onpopstate', 'onpopstate')}}</td> <td>{{Spec2('HTML WHATWG')}}</td> <td></td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <p>{{Compat("api.WindowEventHandlers.onpopstate")}}</p> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{domxref("window.history")}}</li> <li><a href="/ja/docs/Web/Guide/DOM/Manipulating_the_browser_history">ブラウザーの履歴の操作</a></li> <li><a href="/ja/docs/Web/Guide/DOM/Manipulating_the_browser_history/Example">AJAX ナビゲーションの例</a></li> </ul>