JavaScript onunload Event18 Mar 2025 | 4 min read When a user tries to load a page but it is unsuccessful, the page remains unloaded, and a JavaScript onunload event is triggered. The possibility exists that the onunload event will also happen if the browsers have been closed while the page is loaded. When a user goes away from a page while selecting, submitting, clicking, and leaving the browser window, the onunload event typically enters the onunload state. Additionally, this incident happens when a user is trying to load a url orweb browser and accesses data while it is loaded. Syntax The onunload event uses for the html page using the javascript function. The html page and javascript function syntaxes show below.
Add attribute and value in html container or tag.
Use the html attribute name in javascript with function. This attribute has a single value script that executes when the event onunload is triggered. Examples of onunload in JavaScriptThe following examples show the onunload event and its output. Example 1 The onunload JavaScript function is demonstrated in this program, where the user first uses it to call a web page for its function, then tries to close the window to see if the function has been loaded again using the script, and finally assigns an alert function using the script as indicated in the output. Output The following output shows unonload event information in the alert function. ![]() Example 2 The onunload JavaScript function works in the example with the console method. The output of the onunload function's event is displayed in the console tab, and html page output is displayed on the web page. Output The following output shows unonload event information in the console function. ![]() Example 3 the output of the onunload function's event is displayed in the console tab, and html page output is displayed on a web page. We can see the onload function with the object of the id. The event works on the body tag with the console tab. It does not work on the alert function. Output The following output shows unonload event information in the console function. ![]() Example 4 The information on the onunload function's output is displayed in the console tab but not on the html page. Here, we use the windows attribute with the function for reloading the browser. It works on the console tab but does not work on the alert function. Output The following output shows unonload event information in the alert function. ![]() Example 5 The onunload functions used in the html attribute and displayed as an output. The function works on the console tab with function. Output The following output shows unonload event information in the alert function. ![]() Activating the onunload EventThe following triggers are used to activate the onunload event in the javascript.
Web Browsers SupportedThe list of supported browsers for the onunload event property is as follows:
ConclusionThe onunload event triggers the page and displays information when the page remains unloaded. It helps to refresh and reload the web page or browsers. Next TopicJAVASCRIPT TRIGGER CLICK |
In this article, we will understand the gif player using CSS and JS. The GIF Player library enables you to play, pause an animated GIF file just like a video player. It works in modern browsers and IE8+. Following are the various examples of gif player...
7 min read
Sorting characters in a string is a common task in programming, especially in web development. In JavaScript, there are various ways to sort characters in a string. In this article, we will explore some of the most popular techniques to sort characters in a string...
3 min read
The querySelector is a JavaScript method that plays a vital role in the searching of elements. In this section, we will understand and discuss the querySelector () method, its use and also look over an example to understand the concept of the querySelector () method practically. Introducing...
5 min read
JavaScript is an asynchronous (non-blocking) and single-threaded programming language, meaning only one process can be run at a time. In programming languages, callback hell generally refers to an ineffective way of writing code with asynchronous calls. It is also known as the Pyramid of Doom. The callback...
12 min read
JavaScript confirm method invokes a function that asks the user for a confirmation dialogue on a particular action. The confirm () method uses a window object to invoke a dialogue with a question and two option buttons, OK and Cancel. If the user selects the...
3 min read
Introduction: The Document Object Model (DOM) is an essential idea in HTML and XML. HTML frequently uses DOM manipulation for a number of reasons. HTML elements can be added or removed using DOM manipulation. JavaScript allows us to add or remove properties, classes, and IDs from...
6 min read
In this article, we will understand the how to create Presentation using JavaScript. A presentation is used to convey information. It separates your HTML content into several slides so that the visitors are able to navigate between these slides with certain operations such as mouse...
13 min read
JS First Class Function JavaScript is a dynamic scripting language that allows us to use it in the form of an object-oriented style or functional style. In the same way, one such feature of JavaScript is the First class function. In this section, we will discuss the...
4 min read
We often come across charts on analytics-based sites or some websites that contain surveys on some common agendas like population, percentage, and whatnot. These charts are often used in the Data Interpretation field to create network shares or profit distribution in core sectors. However, if we...
11 min read
An Http cookie, also known as a web cookie, is a small piece of data that the server sends to the web browser. The browser may store the cookies and send them back to the server with other requests. The typical use of the cookies...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India