Closing a Window


if (!newwindow.closed) newwindow.close(); 

Closing a window can be done by calling the window's close() method. However, by default this is possible only for windows that were opened using JavaScript; if you try this on other windows, the user gets a warning message similar to the one shown in Figure 9.4 (Mozilla browsers have a similar message, but hide it in the JavaScript console).

Figure 9.4. Close only the windows opened with JavaScriptor cope with this frightening message.


With the sitemap from the previous phrase, a link to close the window comes in handy:

The Sitemap with the Close Link (sitemapwindow.html)

[View full width]

<script language="JavaScript"   type="text/javascript"> this.opener.name = "mainWindow"; </script> <a href="http://www.samspublishing.com/"   target="mainWindow">Publisher</a><br /> <a href="http://www.amazon.com/gp/product/0672328801" target="mainWindow">This book at  Amazon</a><br /> <a href="javascript:self.close();">Close window</a> 

However, you should also close the sitemap from the main window if another page is loaded there. In the current example, loading any other page in the main window redirects the browser off to another web server; therefore, you can close the sitemap immediately. But before doing so, you should check whether the window is already closed, by taking a look at its closed property:

Opening the Sitemap with the Auto-Closing Feature (sitemapwindow.html)

<script language="JavaScript"   type="text/javascript"> var newwindow = window.open(   "sitemapwindow.html",   "sitemap",   "width=320,height=100,directories=no,menubar=no, toolbar=no,scrollbars=yes"); </script> <body onunload="if (!newwindow.closed) newwindow.close();"></body> 




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net