Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
jQuery | unload() Method
Next article icon

jQuery | unload() Method

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The

unload()

method in jQuery is used to perform unload event when the user try navigates away from current webpage.The event can be triggered when user changes the dynamic state of the page for example user clicked on link to leave the page, a new URL is typed in the address bar etc. the unload method should only be use on window object. it specifies what will happen when a unload event occurs.

Syntax:

$(selector).unload(function)

Parameters

This method accepts only one and required parameter described below:

  • Function:It is an required parameter which is used to specify the function to run when the unload event is triggered.

Example-1:

This example describes the triggering of unload event when you click on link.

html
<!DOCTYPE html> <html>  <head>     <title>         jQuery unload() Method     </title>      <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">     </script> </head>  <body style="text-align:center;">     <br/>     <br/>     <br/>     <h1 style="color:green;">            GeeksForGeeks        </h1>      <p>When you click       <a href="https://www.geeksforgeeks.org/">         Go to geeks</a>, or close the window,       an alert box will be triggered.</p>      <!-- Script to illustrates unload() method -->     <script>         $(document).ready(function() {             $(window).unload(function() {                 alert("you are leaving from page");             });         });     </script> </body>  </html> 

Output:

  • Before click anywhere:
  • After clicking on link or try to navigating away from the page

Example-2:

This example describes the triggering of unload event when you click on link.

html
<!DOCTYPE html> <html>  <head>     <title>         jQuery unload() Method     </title>      <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">     </script>      <style>         h1 {             border: 1px solid black;             height: 100px;             padding-top: 35px;             background: green;             color: white;         }     </style> </head>  <body style="text-align:center;">      <h1>GeeksForGeeks</h1>      <!-- Script to illustrates unbind() method -->     <script>         $(document).ready(function() {             $(window).unload(function() {                 alert("you are leaving from page");             });         });     </script>     <p>When you click <a href=     "https://www.geeksforgeeks.org/community/">GeeksForGeeks</a>,    or close the window, an alert box will be triggered.</p>  </body>  </html> 

Output:

  • Before click anywhere:
  • After clicking on link or try to navigating away from the page

Note:

  • The working of unload event depeandent on browsers.
  • The unload() method was removed in version 3.0.
  • The unload() method was deprecated in jQuery version 1.8.

Next Article
jQuery | unload() Method

A

ashishsaini3
Improve
Article Tags :
  • Web Technologies
  • JQuery
  • jQuery-Methods

Similar Reads

    jQuery | unbind() Method
    The unbind() Method is an inbuilt method in jQuery which is used to remove any selected event handlers. This method can be used to remove particular event handler, or stop specific functions. It works on any event handler using an event object. Note: If no parameters are provided, the method works o
    2 min read
    jQuery undelegate() Method
    The jQuery undelegate() method is an inbuilt method that is used to remove the specified event handler from the selected element. Syntax: $(selector).undelegate(selector, event, function)Parameters: This method accepts three parameters as mentioned above and described below: selector: It is an optio
    2 min read
    jQuery | off() Method
    The off() Method in jQuery is used to remove event handlers attached with the on() method. The off() method brings a lot of consistency to the API and it replace unbind(), die() and undelegate() methods. Syntax: $(selector).off(event, selector, function(eventObj), map) Parameter: This method accepts
    2 min read
    jQuery remove() Method
    The remove() method in JQuery is used to remove all the selected elements including all the text. This method also remove data and all the events of the selected elements. Syntax: $(selector).remove()Return Value: It will return all the data of the selected elements deleted. Example 1: Input: $("p")
    2 min read
    jQuery | get() Method
    In jQuery .get() method loads data from the server by using the GET HTTP request. This method returns XMLHttpRequest object. Syntax $.get( url, [data], [callback], [type] ) Parameters url : String containing the URL at which request is to be sent data : This is an optional parameter that represents
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences