29
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryでブラウザの戻るボタンを禁止する

Last updated at Posted at 2018-01-15

#ブラウザの戻るボタンの禁止

昔はhistory.back(-1)なんて誤魔化したもんですが(動作上は一瞬戻って進む)、今はちゃんとヒストリが操作出来るんですね。
ブラウザの履歴を操作する - ウェブデベロッパーガイド | MDN

社内システムなんかで便利です。

history.pushState(null, null, null);
$(window).on("popstate", function (event) {
    if (!event.originalEvent.state) {
        history.pushState(null, null, null);
        return;
    }
});

古いjQuery(1.7未満)を使わざるを得ない場合、下記で。

history.pushState(null, null, null);
$(window).bind("popstate", function (event) {
    if (!event.originalEvent.state) {
        history.pushState(null, null, null);
        return;
    }
});
29
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?