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.
5
  • 1
    Does this answer your question? Check if page gets reloaded or refreshed in JavaScript Commented Feb 13, 2020 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)? Commented Feb 13, 2020 at 18:11
  • I don't think so, @Rapsssito . I am rather searching for a manual (a human being doing something) way. Commented Feb 13, 2020 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. Commented Feb 13, 2020 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.' Commented Feb 13, 2020 at 19:45

1 Answer 1

-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);
});

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.