The Prospect
The Prospect

Reputation: 1

Warn user before leaving Website with jQuery

I need a code that warns/alerts the user if they want to stay on or leave the Website when they click on a link that goes to another Website. I tried this and nothing happens.

Upvotes: 0

Views: 59

Answers (3)

Sparrow
Sparrow

Reputation: 2583

Use the 'on before unload' event:

<script type="text/javascript">
    window.onbeforeunload = onbeforeunload_handler;
    window.onunload = onunload_handler;
    function onbeforeunload_handler() {
        // Do something here
    }

    function onunload_handler() {
       // Or do something here
    }
</script>

Upvotes: 0

Peterson Marques
Peterson Marques

Reputation: 1

window.onbeforeunload = ConfirmExit;

function ConfirmExit() { return "closed...."; }

Upvotes: 0

Aleksander Rezen
Aleksander Rezen

Reputation: 927

$( window ).unload(function() {
  return "Bye now!";
});

more here https://api.jquery.com/unload/

Upvotes: 1

Related Questions