How to check page is reloading or refreshing using jquery or javascript?

前端 未结 4 1038
说谎
说谎 2020-12-06 00:43

I have to do some kind of operation on the page refresh or reload. that is when I hit next page or Filter or refresh on the grid. I need to show some confirmation box over t

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 01:46

    If it is refreshing, window.onunload will fire.

    // From MDN
    window.onunload = unloadPage;
    function unloadPage()
    {
        alert("unload event detected!");
    }
    

    https://developer.mozilla.org/en/DOM/window.onunload

    If you just want a confirmation box to allow them to stay, use this:

    window.onbeforeunload = function() {
        return "Are you sure you want to navigate away?";
    }
    

提交回复
热议问题