.unload()

In this chapter you will learn:

  1. Syntax and Description for .unload() event
  2. How to listen to the unload event

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><!--  j  av  a 2 s  .c o  m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $(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

Next chapter...

What you will learn in the next chapter:

  1. Syntax for .animate()
  2. Animate more than one attribute
  3. Animate canvas
Home » jQuery » 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()