Detect if page is load from back button

后端 未结 7 842
无人及你
无人及你 2021-02-06 23:07

Is there any way to detect if current page came from back button?

I want to load data from cookie only if current page came from back button.

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 23:38

    In addition to @Radderz answer, I had to add setTimeout to make their solution work in chrome 83. Otherwise, I would only see 'First time load' alert.

    document.addEventListener('DOMContentLoaded', function () {
    
        var ibackbutton = document.getElementById("backbuttonstate");
    
        setTimeout(function () {
    
            if (ibackbutton.value == "0") {
                // Page has been loaded for the first time - Set marker
                ibackbutton.value = "1";
                alert('First time load');
            } else {
                // Back button has been fired.. Do Something different..
                alert('Previously loaded - Returned from Back button');
            }
    
        }, 200);
    
    }, false);
    

提交回复
热议问题