jQuery .unload() event handler

Syntax and Description

.unload(handler)

handler is a function to execute when the event is triggered. Return value is the jQuery object, for chaining purposes.

This method is a shortcut for .bind('unload', handler). It binds an event handler to the unload JavaScript event.

Any unload event handler should be bound to the window object.


$(window).unload(function() {
    alert('called.');
});

Display an alert when a page is unloaded


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

Unload event

The following code listens to the unload event from the window.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!-- ww w.  j a va2  s  .  c  o m-->
            $(window).unload( function () { alert("Bye now! java2s.com"); } );
        });
    </script>
  </head>
  <body>
    <body>
    <form action="javascript:alert('success!');">
        <div>
          <input type="text" />
          <input type="submit" />
        </div>
     </form>
     <div></div>
    </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities