12

This code runs fine on Firefox, but I can't make the unload event work on Chrome anymore. Did Chrome stop supporting the unload event?

This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

<script type="text/javascript" src="../jquery/jquery.js"></script>

<script type="text/javascript">

    function pageHidden(evt) { alert("Are you sure 1?"); } //WORKS ON FIREFOX BUT NOT IN CHROME
    window.addEventListener("pagehide", pageHidden, false);

    window.onunload = function () { alert("Are you sure 2?"); } //TRIGGERS ON LOAD NOT ON UNLOAD

    $(window).unload(function () { //WORKS ON FIREFOX BUT NOT IN CHROME
        alert("Are you sure 3?");
    });

</script>
</head>

<body>
TEST WEBSITE
<a href="http://www.iamawesome.com">external link</a>
</body> 
</html>

How can I get the unload event to work in Chrome?

Thanks!


ANSWER: Don't test the unload event with alerts ;)

John Shepard
  • 937
  • 1
  • 9
  • 27
  • 1
    Possible dupe: http://stackoverflow.com/questions/803887/can-i-pop-up-a-confirmation-dialog-when-the-user-is-closing-the-window-in-safari – cwharris Aug 29 '12 at 16:37
  • you're not triggering anything anywhere in your code. you're just binding handlers. – jbabey Aug 29 '12 at 16:39
  • Check [this bug report](http://bugs.jquery.com/ticket/10509). In short, it won't work in Chrome; try to use `.onunload` handler instead, but even that might be 'fixed' as well. ) – raina77ow Aug 29 '12 at 16:39

4 Answers4

13
window.onunload = alert("Are you sure 2?");

This is incorrect. You are setting onunload to the result of alert, you need to set it to a function:

window.onunload = function(){
    alert("Are you sure?");
}

If you want to use jQuery, this will work in all browsers.

$(window).unload(function () {
     alert("Are you sure?");
});

NOTE: It might seem like it's not working in Chrome, but it is. That's because Chrome blocks alerts in the onunload event.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
2

Try:

window.onbeforeunload = function () {
    // code
};

or

window.onpagehide = function () {
    // code
};
Korikulum
  • 2,529
  • 21
  • 25
  • Thanks Korikulum! I tried this but it is not displaying the alert message I write inside each function – John Shepard Aug 29 '12 at 16:45
  • onpagehide is a useful event to know about, but you can't trust it to get called every time a page goes away. For example, if you launch a new tab, and then close the whole browser, the event may not fire. – Bert Cushman Jan 24 '17 at 18:13
2

From what I've read it seems Chrome blocks alerts once that event has been triggered. You can run some functions, however, just not anything that interacts with the user it seems.

From window.onbeforeunload in Chrome: what is the most recent fix?, it seems, if all you want to do is pop up a confirmation message, you have to do it by returning a string with the message from the function you set as the callback.

window.onbeforeunload = function() {
    // Some wrap up code (no alerts, confirms, redirects, etc)
    return 'My confirmation messsage'; 
}

The text "My confirmation message" will then show up in a confirmation dialogue of Chrome's choosing. Firefox documents this behaviour here.

Community
  • 1
  • 1
jeteon
  • 3,471
  • 27
  • 40
0

You don't trigger anything - you just execute the expression which you want to add as a handler:

window.onunload = function(event) {
    alert("Are you sure 2?");
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375