Javascript – Execute function before refresh

browserjavascriptjquerypage-refreshyui

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.

Best Solution

    window.onbeforeunload = function(event)
    {
        return confirm("Confirm refresh");
    };