Solved

Prevent window close

Posted on 1998-10-26
5
342 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?
0
Comment
Question by:vogtster
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1272972
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
 

Author Comment

by:vogtster
ID: 1272973
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
 
LVL 4

Expert Comment

by:martinag
ID: 1272974
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
 
LVL 8

Accepted Solution

by:
jbirk earned 50 total points
ID: 1272975
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
 

Author Comment

by:vogtster
ID: 1272976
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question