18

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 this Events.

is there any event which can tell you page is doing filer? refresh or paging? using javascript?

Thanks

mridul raj
  • 75
  • 1
  • 8
user957178
  • 631
  • 4
  • 15
  • 27

4 Answers4

21

If it is refreshing (or the user is leaving the website/closing the browser), 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?";
}
A-Tech
  • 806
  • 6
  • 22
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • 1
    But it doesn't answer the question. The question is to check specifically if the page is reloading and capture that event. In this case, it will still fire if the browser is closed, nagging the user to confirm before they can close the browser. This is bad behavior, which any competent developer would know not to do. – user4593252 Aug 26 '15 at 13:12
  • 1
    Sure it's bad behavior, but the OP wanted to detect when a page was being refreshed and now he can. Should he? That's his prerogative. – AlienWebguy Aug 26 '15 at 20:16
3

You can create a hidden field and set its value on first page load. When the page is loaded again, you can check the hidden field. If it's empty then the page is loaded for the first time, else it's refreshed. Some thing like this:

HTML

<body onLoad="CheckPageLoad();">

    <input type="hidden" name="visit" id="visit" value="" />

</body>

JS

function CheckPageLoad() {
    if (document.getElementById("visit").value == "") {
        // This is a fresh page load
        document.getElementById("visit").value = "1";
    }
    else {
        // This is a page refresh
    }
}​
Dimitar
  • 484
  • 1
  • 6
  • 16
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Hmm, but the value will always be emptied when you refresh. So it can never determine if the field previously had a value. – Shawn Rebelo Apr 08 '15 at 19:54
  • This might be okay if the page stores the state of controls in a viewstate. The problem is how to address a more complex page that causes postbacks. If this is a simple page that relies on user interaction that could then place a value in that field, then this is a great solution. If, however, the page causes multiple postbacks with lots of fields for the user to fill out, it becomes unscaleable. – user4593252 Aug 26 '15 at 13:14
1

There are some clarification notes on wrestling with this I think are critical.

First, the refresh/hidden field system works on the beginning of the new page copy and after, not on leaving the first page copy.

From my research of this method and a few others, there is no way, primarily due to privacy standards, to detect a refresh of a page during unload or earlier. only after the load of the new page and later.

I had a similar issue request, but basically it was terminate session on exit of page, and while looking through that, found that a browser treats a reload/refresh as two distinct pieces:

  1. close the current window (fires onbeforeunload and onunload js events).
  2. request the page as if you never had it. Session on server of course has no issue, but no querystring changes/added values to the page's last used url.

These happen in just that order as well. Only a custom or non standard browser will behave differently.

1
$(function () {
    if (performance.navigation.type == 1) {
        yourFunction();
    }
});

More about PerformanceNavigation object returned by performance.navigation

Dmitry
  • 6,716
  • 14
  • 37
  • 39
Aurora
  • 725
  • 1
  • 8
  • 17
  • 1
    That's deprecated. There's a replacement but you're not really using any of these interfaces as intended. https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigation – philw Jan 29 '19 at 11:32