We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Prevent window close

vogtster
vogtster asked
on
Medium Priority
400 Views
Last Modified: 2010-08-05
How can I trap and prevent the window from being closed when the user clicks on the X in the upper right-hand corner?

I know that <body onUnload='somecode'> is used to trap the event, but how do I cancel the window close?
Comment
Watch Question

Commented:
I don't think you can prevent the window from being closed, but you could open it again:

<BODY onUnload="open('page.htm')">

Martin

Author

Commented:
I tried that and it doesn't work because the page is not yet unloaded, preventing it from being loaded again.  That would work if there were an "afterUnload" event, but there isn't.

Is there some form of "return = false;" function on "onUnload"?

Commented:
You could set a time out using say .5 seconds:

<BODY onUnload="setTimeout('open(\'page.htm\')', 500)">

There is no "return true" as there is in Java.

Martin
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
To prevent the complications jbirk mentions, I have decided to use an alert in the onUnload event to inform the user that they should not manually close the window, close their browser, and start over.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.