1
window.onunload = function()
{
    confirm("close the window?")
}

Why don't I have the confirm window coming out when closing a window?

PttRulez
  • 115
  • 7
  • window.onunload is not universally supported and where it is, its implementation can vary. Don't rely on it. –  Apr 29 '14 at 06:52
  • Try to use "onbeforeunload" instead. Don't use `confirm`, just return a string from the event handler. The browser will take care of the confirmation message box all by itself, presenting the message you returned from the event handler – devnull69 Apr 29 '14 at 06:53

3 Answers3

0

Presumably it is to prevent pages from repeatedly preventing you from closing a page. With the beforeunload event, you can, however, get what is, in most modern browsers, a non-customizable or only partly customizable dialog prompting the user whether to leave or not.

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
0

Many browsers not support window.unload.

Please view this link.

window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?

Community
  • 1
  • 1
HarisJayadev
  • 92
  • 12
0

Most browsers prevent the use of alert/confirm in onunload. Instead use an onbeforeunload handler that returns a string:

window.onbeforeunload = function() {
    return "Don't go!";
};

This will then be presented to the user in a dialogue box.

See also: window.unload() won't work in jQuery

Community
  • 1
  • 1
James Hibbard
  • 16,490
  • 14
  • 62
  • 74