Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
31
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@masashi68

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

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

昔は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;
    }
});
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
31
Help us understand the problem. What are the problem?