Back

Unload event

What is the unload event ?

The unload event occurs when the user interups the loading of a page by leaving the page; or when the user clicks on a link, sends a form, closes the browser window or returns to the previous page. In contrast, the "load" event occurs when the page has finished loading. 

Why is this important ?

The "onUnload" and "onLoad" attributes are embedded in the <body> tags in HTML or <frameset> in JavaScript.
However, the unload event does not work, especially on cell phones, when :

  • you close the tab of a web page from the tab selector 
  • you close the browser application from the application selector

In addition, the presence of an "unload" event can prevent browsers from caching or placing pages in the background while browsing.

For example : If you are on a web application that plays a video and you decide to view another application at the same time, the unload event can prevent the application containing the video from stopping. The unload will prevent that application from being considered as background, which will not allow the video to be interrupted by switching to another application or web page.

How do I fix this problem ?

It is recommended on all modern browsers to use the "pagehide" event to detect page unloads.