« Learning to Like the Var Keyword | Main | Session Variables vs. Hidden Inputs »

July 22, 2003

Using JS to Close a Window You Didn't Open

We all know it's impossible to use JavaScript to close arbitrary windows, right? One of the first things you learn when playing with opening and closing windows with JavaScript is that, for security reasons, you can't close a window that you didn't open.

Well, as it turns out, you can, as I recently learned from this post on ASP Alliance. I won't reveal the secret here, but if you want proof that it works, here it is:

This is a traditional attempt to close the main browser window. It shouldn't work in most browsers -- at least not without prompting. This, on the other hand, is an attempt that should work. Goodbye.

Posted by cantrell at July 22, 2003 11:42 AM

Comments

Doesn't work in Mozilla. Works in IE6. Interesting.

Posted by: dgibson at July 22, 2003 1:35 PM

That's why I love Mozilla... :)

Posted by: Jonas Galvez at July 22, 2003 2:07 PM

Just to note: dunno if it works in Mozilla, but it does not in Mozilla Firebird 0.6 :)

Posted by: Jonas Galvez at July 22, 2003 2:08 PM

I just discovered that Safari will close the window with a simple self.close(). I'm now thinking this code is really nothing more than interesting trivia and not something you really want to leverage outside of an intranet solution or something.

Posted by: Christian Cantrell at July 22, 2003 2:26 PM

Didn't we did this in Flash about a year go, I think it is punctuated across the macromedia web forum, if you care to search.
getURL("javascript: window.opener=self; window.close();");

Posted by: Brajeshwar at July 22, 2003 3:59 PM

brajeshwar is right. you must have missed it a year or so ago on flashcoders... get with it christian! ;-)

actually i think you're right that this is certainly not an everyday use function for sites. *shudder* though it does have it's place (we have used it in intranet apps to much success).

a perhaps more interesting IE-only trick i posted to Flashcoders also some time ago is how to actually catch the browser closing and allowing a little time for a flash movie to clean itself up -- like talking back to the server, or writing sharedObject data, etc. -- before actually closing the window.

http://chattyfig.figleaf.com/ezmlm/ezmlm-cgi?1:mss:53157:200211:ooebcihhgdmknobojfdk

watch the word wrap there. also check the follow-up post for an addendum.

cheers
g.

Posted by: g.wygonik at July 23, 2003 12:35 AM

I guess we ColdFusion developers are about a year behind the Flash community. :)

Christian

Posted by: Christian Cantrell at July 23, 2003 12:45 AM

I'm alittle puzzled. I'm using flash buttons to open child windows but my javascript window.close or self.close or even onClick="javascript: window.close()" in the child window isn't working without that stupid IE prompt about closing. Any suggestions?

Posted by: Dreamer at October 28, 2003 3:02 AM

Dreamer, the IE window only prompts about closing if that window was opened by the user (not spawed by a javascript function). Unless a window is created by the javascript window.open function, IE will always give a prompt!

Posted by: online coupons at November 2, 2003 4:37 PM

Brajeshwar, thanks so much for the code!

Posted by: Bob at December 7, 2003 3:01 PM

Hi,

I've been using the following tag with much success for quite some time on a window that was spawned by a javascript function:

I moved my code to a Windows 2003 server and I'm running CF MX 7 and .NET Framework 2.0 and J2SE Runtime 5.0 and it stopped working. It works on every other Win2K server I have, so I don't think it's a coding issue. The form still posts, but the window won't close and I get the ever-popular and ubiquitous Error: Object Expected message from the javascript.

Does anyone have any idea why this might be happening?

Posted by: Jasmine M. at January 26, 2006 10:33 AM




Remember Me?

(you may use HTML tags for style)