You Can't Do That On The Web - Closing Non-Scripted Browser Windows
page 1 of 1
Published: 18 Dec 2003
Unedited - Community Contributed
Abstract
Closing non-scripted windows is like Santa Claus; new developers believe in it until they know better. Well, score one for childhood fantasy.
by Peter Brunone
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 10457/ 44

The Uncanny Disappearing Window
How do you programmatically close a browser window when you didn't open it? Of course, everybody knows you can't. If they don't know that, they're novice developers and some seasoned veteran will set them straight... gently or otherwise.

    Well, this week one of my clients -- whose intranet app opens in a specially formatted window -- told me they didn't want two windows just to start one application. I had very little success removing the toolbars from an existing window, and my vast web development experience told me that I had no other option. Rather, I didn't until a suggestion from a fellow member of the LaTech JavaScript list altered my perspective and resulted in the script I'm about to show you.

    All you need to start the magic is to assign a window to the opener property of the current window. If this attribute is not set, the browser will realize that the current window is not open to your manipulation and nothing will happen.

 <SCRIPT LANGUAGE="JavaScript">
  window.opener = top;
 </SCRIPT>
  

    That's all. The following is a list of browsers that either do or don't work with this code. If you don't see your browser below, let me know and I'll add it to the mix.

Browser Version OS Works?
Internet Explorer 5.0 Windows No
  5.5 Windows Yes
  6.0 Windows Yes
Netscape 4.6 Windows Yes
  4.79 Windows No
  6.2 Windows Yes
  7.0 Windows Yes
Opera 6.03 Windows Yes
  7.11 Windows Yes
OmniWeb 4.1 Mac OS Yes
Mozilla 1.1 Windows Yes
  1.2.1 Windows Yes
  1.4 Windows No
  1.0 Red Hat Linux Yes
Safari 1.0 MacOS Yes

    Before we go further, here are a few warnings: First, use this technique sparingly. It's great for controlled environments, but if you use it in your public sites without telling people first, they probably won't come back to visit. Second, DON'T try the test links below until you bookmark this article. Unless your browser is listed with a "No" above, chances are that the window is about to go bye-bye.

    There are two basic ways to use this technique. The first is simply to close the current window, as shown here:

 <SCRIPT LANGUAGE="JavaScript">
  window.opener = top;
  window.close();
 </SCRIPT>
  

    Bookmark your spot, and try it here.

    Method number two, which I used for my client's intranet app, is to open a new window and then close the old.

 <SCRIPT LANGUAGE="JavaScript">
  window.open("WhereAmI.asp","newWindow","height=520,
   width=730,toolbars=no,scrollbars=yes,resizable=yes");
  window.opener = top;
  window.close();
 </SCRIPT>
  

    Again, be sure you've got that bookmark... and try it here.

    Please send all comments, questions, shattered dreams, etc. to the address below. Thanks for reading, and remember that with great power comes great responsibility. Use it wisely, and watch where you swing your webs :)

peterbrunone@aspalliance.com


User Comments

Title: Won't close   
Name: Mike Hinkle
Date: 2009-11-07 7:41:01 AM
Comment:
I tried this, but I get an error that says that it's missing required atribute type.
Title: Close without prompt   
Name: Peter
Date: 2009-04-01 2:07:57 PM
Comment:
FYI, I have no access to this site anymore. The point of the article was to close a non-js-opened window without getting the confirm dialog box (yes, you can close any window *with* the confirm prompt), but I believe this option was eliminated by almost all browser makers.
Title: Close without prompt of Yes/No   
Name: Rahul
Date: 2009-04-01 12:39:44 PM
Comment:
Hi Peter, first of all, I was able to close a non-js opened window simply by using:

self.close();

Secondly, this question has been asked before also, and you did not answer it. Is it possible to close the window without the user being presented with yes/no confirmation?
Title: How to close without prompt of yes/No   
Name: Herry
Date: 2008-04-15 8:28:55 AM
Comment:
Hi this is quite useful.
But I dont want a popup of Yes/No on closing the window.
Please help me.
Title: Thanks Mucho mucho   
Name: Henry
Date: 2007-05-18 2:11:11 PM
Comment:
You just save my day, this is what I was looking for that drove me crazy trying to figure out how.
Title: Thanks!!!   
Name: Baris
Date: 2006-07-20 5:30:24 AM
Comment:
thanks for this nice tip :)
Title: Thanks so much   
Name: Nguyen Viet Hoang
Date: 2005-07-17 9:56:00 PM
Comment:
Thanks, your article helped me so much :)

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-06 10:11:45 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search