jQuery 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:

ADVERTISEMENT

ADVERTISEMENT

  • 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:

$(selector).unload(function)  

It adds a function to the unload event.

Parameters of jQuery unload() event

ParameterDescription
FunctionIt 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.


  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $(window).unload(function(){  
  8.         alert("Goodbye!");  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <p>When you click <a href="http://www.javatpoint.com/">this link</a>, or close the window,  
  15.  unload event will be triggered.</p>  
  16. </body>  
  17. </html>  

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *