.unload()

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  . ja v  a  2  s .  c  om-->
            $(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

The code above generates the following result.

.unload()
Home »
  Javascript Tutorial »
    jQuery Reference »
      Event
Javascript Tutorial Event
Event
Event attributes
event.keycode
event.clientX/clientY
pageX/pageY
Cancel event
Event target
bind
.blur()
.change()
.click()
.error()
.dblclick()
.delegate()
.die()
.focus()
.hover()
.keydown()
.keypress()
.keyup()
.live()
.load()
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseover()
.mouseout()
.mouseup()
.off()
.on()
.one()
.ready()
.resize()
.scroll()
.select()
.submit()
.toggle()
.trigger()
.triggerHandler()
.unbind()
.undelegate()
.unload()