Solved

Prevent window close

Posted on 1998-10-26
5
338 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now