Introduction:

In this article we will explain how to use unload() method in jquery  or how to show alter message when user leaves the page or how to show alter message when user refresh the page.

Description:

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 and it’s also triggered when user refresh the page.

syntax:

$(selector).unload(function)

jQuery unload() example

Let's take an example to see the unload() event in jQuery.

In the above code when user click on “this link” then windows will be write a log “Close this window to show Me Unload Event occur..!and redirect to another page.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Jquery Tutorials</title>
 <script src="Scripts/jquery-1.8.2.min.js"></script>
</script>
    <script>
        $(document).ready(function () {
            $(window).unload(function () {
                console.log("Close this window to show Me Unload Event occur..!");
            });
        });
    </script>
</head>
<body>
    <p>
When you click <a href="http://www.sqltutorialspoint.com/">this link</a>
, or close the window, unload event will be triggered.
       
    </p> 
</body>
</html>

 

 

Output:


When user click on “this link” then it will be write a log following below:

 

I hope it will help to you after reading it.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like