0

I am designing a HTML page, and I would like to send a simple message (or trigger some action) when the user intentionally request updating (update button on the web browser, pressing F5... or whatever any other manual method that could exist) of the HTML file. Something like:

window.onmanualupdaterequest = alert("You requested update");

Or whatever the correct procedure could be.

How could I do this?

Further notes:

  • I have tried the window.onbeforeunload function (example), but it does not exactly solve the problem (I would say it has not the same behavior as user request).
  • I would like to ignore the autoupdate case (like in setInterval or similar functions or scripts) from the manual update case. This question is about the manual one.
  • The classical Android swipe-down update method for a web page is considered here as a manual update method.
Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52
  • 1
    Does this answer your question? [Check if page gets reloaded or refreshed in JavaScript](https://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript) – Rapsssito Feb 13 '20 at 17:12
  • Are you trying to prevent a user-generated update (before update) or just detect if the page was manually updated (after update)? – F.Igor Feb 13 '20 at 18:11
  • I don't think so, @Rapsssito . I am rather searching for a manual (a human being doing something) way. – Sopalajo de Arrierez Feb 13 '20 at 18:22
  • Both methods would be OK, @F.Igor , as long as they are manually-generated, not started by a script or function. Added info to the original question to reflect. – Sopalajo de Arrierez Feb 13 '20 at 18:24
  • Typically you code your links and anything that alters the page location to disable the onbeforeunload code that causes messages to pop up. There is really no way to deal with it or no what caused the page to unload.' – epascarello Feb 13 '20 at 19:45

1 Answers1

-1

My idea would be to use the sessionStorage to save the window.location.href on pageload. If the user reloads the page the stored location should match the current url:

window.addEventListener('load', function () {
  const lastUrl = sessionStorage.getItem('lastUrl');
  if(lastUrl && lastUrl === window.location.href) {
    alert("You requested update");
  }
  sessionStorage.setItem('lastUrl', window.location.href);
});
Mischa
  • 1,591
  • 9
  • 14
  • I am not sure, Mischa. My objective was rather to address the manual update case, not the autoupdate. It seems your could makes no differences about. – Sopalajo de Arrierez Feb 13 '20 at 18:02
  • Ahh yes .... if u want to filter out automatic reloads you will need to intercept the automatic reloading events and delete the key first ... but i understand that this is not exactly what you asked for. – Mischa Feb 14 '20 at 10:12