15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

164<br />

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

windowObject.open()<br />

shows all the elements necessary to create a new window that has all the right stuff<br />

on most platforms. The new window object reference is assigned to a global variable,<br />

newWindow. Before a new window is generated, the script looks to see if the<br />

window has never been generated before (in which case newWindow would be<br />

null) or, for newer browsers, the window is closed. If either condition is true, the<br />

window is created with the open() method. Otherwise, the existing window is<br />

brought forward with the focus() method (NN3+ and IE4+).<br />

As a safeguard against older browsers, the script manually adds an opener property<br />

to the new window if one is not already assigned by the open() method. The<br />

current window object reference is assigned to that property.<br />

Due to the timing problem that afflicts all IE generations, the HTML assembly and<br />

writing to the new window is separated into its own function that is invoked after a<br />

50 millisecond delay (NN goes along for the ride, but it could accommodate the<br />

assembly and writing without the delay). To build the string that is eventually written<br />

to the document, I use the += (add-by-value) operator, which appends the string<br />

on the right side of the operator to the string stored in the variable on the left side.<br />

In this example, the new window is handed an -level line of text to display.<br />

Listing 16-26: Creating a New Window<br />

<br />

<br />

New Window<br />

<br />

var newWindow<br />

function makeNewWindow() {<br />

if (!newWindow || newWindow.closed) {<br />

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

if (!newWindow.opener) {<br />

newWindow.opener = window<br />

}<br />

// force small delay for IE to catch up<br />

setTimeout(“writeToWindow()”, 50)<br />

} else {<br />

// window’s already open; bring to front<br />

newWindow.focus()<br />

}<br />

}<br />

function writeToWindow() {<br />

// assemble content for new window<br />

var newContent = “One Sub Window”<br />

newContent += “This window is brand new.”<br />

newContent += “”<br />

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

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

newWindow.document.close() // close layout stream<br />

}<br />

<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!