unload()

The jQuery unload() method is used to unload a specific element. It attaches an event handler to unload event. The unload event is sent to the window element when the user navigates away from the page. It was deprecated in jQuery 1.8 version of jQuery library.

Ways to trigger unload event

An unload event is triggered if you:

  • Click on a link which leads to leave the page.
  • Use the forward or back button.
  • Type a new URL in the address bar.
  • Close the browser window.
  • Reload the page.

Syntax:

snippet
$(selector).unload(function)

It adds a function to the unload event.

Parameters of jQuery unload() event

Parameter Description
Function It is an essential parameter. It executes itself when the unload event is triggered.

Example of jQuery unload() event

Let's take an example to demonstrate jQuery unload() event.

snippet
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(window).unload(function(){
        alert("Goodbye!");
    });
});
</script>
</head>
<body>
<p>When you click <a href="http://www.rookienerd.com/">this link</a>, or close the window,
 unload event will be triggered.</p>
</body>
</html>
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +