Execute function before refresh

Wasim Shaikh picture Wasim Shaikh · Feb 16, 2012 · Viewed 51.9k times · Source

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.

Answer

bang picture bang · Feb 16, 2012
    window.onbeforeunload = function(event)
    {
        return confirm("Confirm refresh");
    };