View Full Version : Dialog box on close event

12-21-2007, 07:01 PM
How can I create a dialog box before closing the web browser (IE or Firefox). When a user clicks on the "X" on the upper right hand I need a dialog box to appear and display a message "Have you logged out first? " Yes or No.

Yes Button - Close the browser window and dialog box
No Button - Don't close browser. Close dialog box

Any idea how?

Philip M
12-21-2007, 08:00 PM
<script type = "text/javascript">
window.onbeforeunload = ask;
function ask() {
return "";

But onbeforeunload only works in IE and Firefox.

For those browsers that don't support it, it will do nothing.

12-27-2007, 08:12 PM
Great. It does what I want. But can I customize the message? Something like,

Have you logged off IDC? Yes or No

If yes close window
If no remain open

12-27-2007, 08:31 PM
confirm('any text you want goes here')

will display two buttons - OK and Cancel for your visitor to choose between.

Just remember that the onbeforeunload event is non-standard and only works in IE and Firefox and only if the browser owner has not disabled it (as any sensible person would in order to stop sites from being able to interfere with their ability to leave).