15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

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.

60 <strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

elementObject.blur()<br />

Listing 15-23 (continued)<br />

newWindow = window.open(“”,””,”width=250,height=250”)<br />

// pause briefly to let IE3 window finish opening<br />

setTimeout(“fillWindow()”,100)<br />

} else {<br />

// window already exists, so bring it forward<br />

newWindow.focus()<br />

}<br />

}<br />

// assemble new content and write to subwindow<br />

function fillWindow() {<br />

var newContent = “Another Subwindow”<br />

newContent += “”<br />

newContent += “A Salmon-Colored Subwindow.”<br />

newContent += “”<br />

// the following button doesn’t work in NN6<br />

newContent += “”<br />

newContent += “”<br />

// write HTML to new window document<br />

newWindow.document.write(newContent)<br />

newWindow.document.close()<br />

}<br />

<br />

<br />

<br />

Window focus() and blur() Methods<br />

<br />

<br />

<br />

<br />

<br />

<br />

A key ingredient to the success of the makeNewWindow() function in Listing 15-23<br />

is the first conditional expression. Because newWind is initialized as a null value<br />

when the page loads, that is its value the first time through the function. But after<br />

you open the subwindow the first time, newWind is assigned a value (the subwindow<br />

object) that remains intact even if the user closes the window. Thus, the value<br />

doesn’t revert to null by itself. To catch the possibility that the user has closed the<br />

window, the conditional expression also sees if the window is closed. If it is, a new<br />

subwindow is generated, and that new window’s reference value is reassigned to<br />

the newWind variable. On the other hand, if the window reference exists and the<br />

window is not closed, the focus() method brings that subwindow to the front. You<br />

can see the focus() method for a text object in action in <strong>JavaScript</strong> <strong>Bible</strong> Chapter<br />

25’s description of the select() method for text objects.

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

Saved successfully!

Ooh no, something went wrong!