【JavaScript】ブラウザバック(戻るボタン)を無効化する方法

概要

JavaScriptのHistory APIでブラウザの戻るボタンを無効化することができる。

サンプル

window.history.pushState(null, null, null);
window.addEventListener("popstate", function() {
  window.history.pushState(null, null, null);
  return;
});

処理内容の説明

  • window.history.pushState(null, null, null);
    履歴の先頭にnull(自分自身)を追加。
  • window.addEventListener("popstate", イベントハンドラ);
    戻るボタンが押されたらイベントハンドラが実行される。

【1つ新しい記事】

【1つ古い記事】

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です