Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute function before refresh

On my HTML page, when the user clicks / presses F5 button the page refreshes, but before it refreshes I want to execute a function or a simple alert.

User can click on refresh button, press F5 or Ctrl + R.

Using core JavaScript, jQuery or YUI.

like image 797
Wasim Shaikh Avatar asked Feb 16 '12 09:02

Wasim Shaikh


People also ask

How do you call a function on page refresh?

You need to call myFunction() when the page is loaded. window. onload = myFunction; If you only want to run it when the page is reloaded, not when it's loaded for the first time, you could use sessionStorage to pass this information.

How do you reload a function?

The true keyword force to reload the page from the server, while the false keyword reloads the page from the cache. The false is the default parameter of this method, so if we omitted the parameter's value, the reload() method reloads the page from the cache. It means that the object. reload() is same as the object.

How can I tell if an angular page is reloaded?

There isn't any default way to respond to this inside your Angular application. You might be able to mock something up using onBeforeUnload() to log that the application was reloaded; and then check that log in your application's initialization routine to perform some special action.

How do you refresh JavaScript?

You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.


1 Answers

    window.onbeforeunload = function(event)     {         return confirm("Confirm refresh");     }; 
like image 137
bang Avatar answered Sep 16 '22 15:09

bang