[Solved] window.onunload is not working properly in Chrome browser. Can any one help me?

I have written this code

function winUnload() {
    alert("Unload Window");

window.onunload = function() { winUnload(); }

This code is working fine in IE and Firefox. But this code is not working in Chrome. Both the statements alert("Unload Window"); and MyMethod(); are not working.

Enquirer: Imran


Solution #1:

There are some actions which are not working in chrome, inside of the unload event. Alert or confirm boxes are such things.

But what is possible (AFAIK):

  1. Open popups (with – but this will just work, if the popup blocker is disabled for your site
  2. Return a simple string (in beforeunload event), which triggers a confirm box, which asks the user if s/he want to leave the page.

Example for #2:

$(window).on('beforeunload', function() {
    return 'Your own message goes here...';


Respondent: Armin

Solution #2:

I know this is old but I found the way to make unload work using Chrome

window.onbeforeunload = function () {
Respondent: Carlos

Solution #3:

Armin’s answer is so useful, thank you. #2 is what’s most important to know when trying to set up unload events that work in most browsers: you cannot alert() or confirm(), but returning a string will generate a confirm modal.

But I found that even with just returning a string, I had some cross-browser issues specific to Mootools (using version 1.4.5 in this instance). This Mootools-specific implementation worked great in Firefox, but did not result in a confirm popup in Chrome or Safari:

window.addEvent("beforeunload", function() {
    return "Are you sure you want to leave this page?";

So in order to get my onbeforeonload event to work across browsers, I had to use the JavaScript native call:

window.onbeforeunload = function() {
    return "Are you sure you want to leave this page?";

Not sure why this is the case, or if it’s been fixed in later versions of Mootools.

Respondent: Tracy

Solution #4:

You may try to use pagehide event for Chrome and Safari.

Check these links:

How to detect browser support for pageShow and pageHide?

Solution #5:

Please try window.onbeforeunload instead for window.onunload for chrome.
You can also try calling onbeforeunload from the body> tag which might work in chrome.

However, we do have a problem with unload function in chrome browser.
please check

location.href does not work in chrome when called through the body/window unload event

Respondent: Nivedittan

Solution #6:

The onunload event won’t fire if the onload event did not fire. Unfortunately the onload event waits for all binary content (e.g. images) to load, and inline scripts run before the onload event fires. DOMContentLoaded fires when the page is visible, before onload does. And it is now standard in HTML 5, and you can test for browser support but note this requires the <!DOCTYPE html> (at least in Chrome). However, I can not find a corresponding event for unloading the DOM. And such a hypothetical event might not work because some browsers may keep the DOM around to perform the “restore tab” feature.

The only potential solution I found so far is the Page Visibility API, which appears to require the <!DOCTYPE html>.

Respondent: Shelby Moore III

Solution #7:

This works :

var unloadEvent = function (e) {
    var confirmationMessage = "Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?";
    (e || window.event).returnValue = confirmationMessage; //Gecko + IE
    return confirmationMessage; //Webkit, Safari, Chrome etc.
window.addEventListener("beforeunload", unloadEvent);
Respondent: Amulya Kashyap

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy