15
19

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.

ブラウザバック時にリロードする

Posted at

「test1.html → test2.html → test3.html」の画面遷移の中で
test3.html から test2.htmlにブラウザバックで戻った際に強制的に1回リロードさせるjsです。
なお、<button class="back">Back</button>で戻った場合にはリロードしないようにしてあります。

test1.html
<button class="next">Next</button>
test2.html
<button class="next">Next</button>
<button class="back">Back</button>
test3.html
<button class="next">Next</button>
<button class="back">Back</button>
js
$(function () {
    //test1でnextをクリックする→読み込み済み(loaded)
    if(location.pathname=="test1") {
        $(".next").click(function(){
            window.name = "loaded";
        });
    }

    //test2で未読み込み(null)であればリロード
    //test2で読み込み済み(loaded)であればリロードしない
    //test2でnextをクリックする→未読み込み(null)
    if(location.pathname=="test2") {
        if (window.name != "loaded"){
            location.reload();
            window.name = "loaded";
        }
        $(".next").click(function(){
            window.name = "null";
        });
    }

    //test3でbackをクリックする→読み込み済み(loaded)
    //test3でブラウザバックをする→未読み込み(null)
    if(location.pathname=="test3") {
        $(".back").click(function(){
            window.name = "loaded";
        });
    }
});
15
19
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
15
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?