Sergio Romero Sergio Romero - 1 year ago 181
jQuery Question

$(window).unload is not firing

I want to execute an action method when the user is abandoning a particular page using jQuery.

The page has the following code:

<script type="text/javascript">

$(window).unload(function () {
alert("Handler for .unload() was called.");


When I navigate away from the page I never see the expected alert.

Answer Source

Actually some browsers such as Google Chrome might block if you attempt to alert in a window unload. As a user I like this feature. Alerting everytime you try to navigate away from a page sucks:

enter image description here

Replace the alert with a console.log or something else less intrusive to the user and the event will be happily called.

You might also want to checkout the onbeforeunload event.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download