Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Prevent window close

Posted on 1998-10-26
5
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

715 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