×

jQuery Tutorial

jQuery Examples

jQuery Practice

jQuery unload() Method

jQuery | unload() Method: Learn about the jQuery unload() Method with its usages, syntax, and examples.
Submitted by Pratishtha Saxena, on October 20, 2022

unload() Method

Events in jQuery are the actions that the user performs on the web page. It can be anything – related to mouse clicks, keyboard presses, etc. Using jQuery, we can control these events in the order we want and can also attach some custom functions to it if needed. That means, we can use predefined event methods for the actions and also define a function that gets fired when the event method is triggered. Overall, this makes the website more dynamic on the user's end. Let's learn about the unload() method here.

The unload event gets triggered when the page is either refreshed or has been left. When the user either goes to some other page or goes back to the previous page, then the current page gets unloaded. The unload() method is used on the windows object.

unload() Method Syntax

$(selector).unload(function);

It takes a function as its parameter. The function is fired when the unload event is triggered on the window.

Note: Unload event has been deprecated and then further removed from version 3.0. Therefore, now we use .on() and .trigger() method to unload the URL of the page.

jQuery unload() Method Example

<!DOCTYPE html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  </head>
  
  <body>
    <h2>jQuery Event - UnLoad</h2>
    <hr>
    <iframe src="https://cdn.pixabay.com/photo/2016/10/26/19/00/domain-names-1772240_960_720.png" height="200px"></iframe>
    <hr>
  </body>
  
  <script type="text/javascript">
    $(window).unload(function(){
        alert('Page had been Unloaded !!!');
    });
  </script>
</html>

Output:

Example 1: jQuery unload() Method


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.