Detection of reload button pressed in Java Script

Viewed 65

How to detect whether the reload button is pressed and after that the the processes be held and after that reload. Like when reload is pressed or Ctrl + R then I want to save that it is reloaded in local storage.

2 Answers

The event handler for processing onbeforeunload events is the WindowEventHandlers mixin's onbeforeunload property. When a window is about to unload its resources, these events are triggered.

window.onbeforeunload = function(m) {
  return 'Your Statement';
};

And you can refer to Mozilla docs for more because you didn't shared your source code. https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

To save localStorage in browser on reload/refresh of page.

Add Javascript code as below

window.onload = function () {
    alert(localStorage.variable) //comment this line to avoid alert box
    localStorage.setItem("variable", "set a variable value");
}
Related