javascript
  1. javascript-unload

JavaScript unload

The unload() method is a JavaScript method, which is used to unload the current document.

Syntax

window.onunload = function() {
   // code here
};

Example

window.onunload = function() {
   alert("The document is being unloaded");
};

Output

When the user closes the page or navigates to a different page, the alert message will be displayed.

Explanation

The unload() method is triggered when the user closes the document window or navigates to a different website. This method is used to perform actions or execute script before the document is unloaded.

Use

The unload() method can be used to perform some actions before the document is unloaded. For example, it can be used to save user data, clear the cookies, or perform some cleanup actions.

Important Points

  • The unload() method is not supported in some browsers, such as Internet Explorer.
  • The unload() method may not work as expected in some mobile devices.
  • The unload() method is often used in combination with the onload() method, which is used to load a document.

Summary

In this tutorial, you learned about the unload() method in JavaScript. You learned about its syntax, example, output, explanation, use, important points, and summary. The unload() method is useful for performing some actions before the document is unloaded, but it has some limitations and may not work in some browsers or mobile devices.

Published on: