Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

Prevent window close

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?
0
vogtster
Asked:
vogtster
  • 2
  • 2
1 Solution
 
martinagCommented:
I don't think you can prevent the window from being closed, but you could open it again:

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

Martin
0
 
vogtsterAuthor 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"?

0
 
martinagCommented:
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
0
 
jbirkCommented:
This works (I tried it):
<BODY onUnload="window.open(location,'_blank');">

The only problem is that onUnload is triggered for a lot more events than just close.  Like changing urls by following a link, choosing a bookmark, clicking the home button.  Also simply hitting reload triggers the onUnload event.  This makes a avery annoying page that just won't go away!  If someone did that to me, I'd definitely never come back!

Now there may be another solution to this that's possible.
There is a property of window objects called closed.  You can test this via another window to determine if the window had been closed.

So you could do something like this in the page which you want to keep it from being closed:
<BODY onLoad="new_win=window.open('test_close.html','test_close','width=100,height=100'); new_win.opener=self; new_win.myURL=location.href;" onUnload="new_win.test_close();">


And then in the test_close.html page, you simply put this code:
function test_close()
{if (opener.closed) //if it's been closed
  window.open(myURL,'_blank');
 else
  setTimeout('test_close()',1000);
}


The only problem is that the user can just close the new window you opened.  If you had another window which you expect to stay open, you could use that instead, and it would have a better chance than a blank window the user doesn't know the purpose for.

Why do you want to do this?  Maybe there is a good solution that goes down a different path.
-Josh
0
 
vogtsterAuthor 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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now