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.

11 Responses to Using JS to Close a Window You Didn’t Open

  1. dgibson says:

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

  2. Jonas Galvez says:

    That’s why I love Mozilla… 🙂

  3. Jonas Galvez says:

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

  4. 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.

  5. Brajeshwar says:

    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();”);

  6. g.wygonik says:

    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. the word wrap there. also check the follow-up post for an addendum.cheersg.

  7. Christian Cantrell says:

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

  8. Dreamer says:

    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?

  9. 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 function, IE will always give a prompt!

  10. Bob says:

    Brajeshwar, thanks so much for the code!

  11. Jasmine M. says:

    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?