O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


WEBMASTER HACK

Using JavaScript to close the opening window
When you try to close a window in JavaScript with self.close(), it asks the end user permission to close the window if it doesn't have a parent window. A simple property change will fix it.

Contributed by:
bconklin2
[09/22/04 | Discuss (3) | Link to this hack]

When you try to close a window in JavaScript with self.close(), the browser asks the end user permission before it will close the window if it doesn't have a parent. This can be irritating, because what if you don't want it to ask permission? What if what you are doing is dynamically creating a new main window?

The way to fix this is very simple. All you have to do is change the value of the opener property of the window class, like this:

window.opener = "Something";

Now you can close it like you normally would, like this:

self.close();

This works because the permissions of the opener property are read/write, making it simple to close the primary window of a site.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.