11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Open Window<br />

<br />

<br />

<br />

Note Be careful that you do not have a pop-up killer installed with your browser, as it may break<br />

the various window creation examples in this chapter. Remember, not all pop-ups are evil.<br />

Once a window is open, the close() method can be used to close it. For example, the following<br />

fragment presents buttons to open and close a window. Make sure to notice the use of the<br />

secondwindow variable that contains the instance of the Window object created.<br />

<br />

<br />

<br />

<br />

This usage of the close() method is rather dangerous. If the secondwindow object does not<br />

exist, you will throw an error. Reload the previous example and click the Close button<br />

immediately and you should get an error. However, if you create a window even once, you will<br />

not see an error regardless of the presence of the window on the screen, because the object<br />

probably will still be in the scope chain. In order to safely close a window, you first need to look<br />

for the object and then try to close it. Consider the following if statement, which looks to see if<br />

the secondwindow variable is instantiated before looking at it and then looks at the closed<br />

property to make sure it is not already closed.<br />

if (secondwindow && !secondWindow.closed)<br />

secondwindow.close();<br />

Note that this previous example actually specifically relies on short circuit evaluation, because if<br />

secondwindow is not instatiated, looking at its closed property would throw an error. <strong>The</strong><br />

following short example shows the safe use of the Window methods and properties discussed<br />

so far.<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!