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.

130<br />

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

windowObject.closed<br />

can be written to it. The delay (using the setTimeout() method described later in<br />

this chapter) invokes the finishNewWindow() function, which uses the global<br />

newWind variable to reference the window for writing. The document.close()<br />

method closes writing to the document — a different kind of close than a window<br />

close.<br />

A separate function, closeWindow(), is responsible for closing the subwindow.<br />

To accommodate Internet Explorer 3, the script appears to create another window<br />

with the same characteristics as the one opened earlier in the script. This is the<br />

trick: If the earlier window exists (with exactly the same parameters and a name<br />

other than an empty string), Internet Explorer does not create a new window even<br />

with the window.open() method executing in plain sight. To the user, nothing<br />

unusual appears on the screen. Things look weird for Internet Explorer 3 users only<br />

if the user has closed the subwindow. The window.open() method momentarily<br />

creates that subwindow. This subwindow is necessary because a “living” window<br />

object must be available for the upcoming test of window existence. (Internet<br />

Explorer 3 displays a script error if you try to address a missing window, while<br />

NN2+ and IE4+ simply return friendly null values.)<br />

As a final test, an if condition looks at two conditions: 1) if the window object<br />

has ever been initialized with a value other than null (in case you click the window<br />

closing button before ever having created the new window) and 2) if the window’s<br />

closed property is null or false. If either condition is true, the close() method<br />

is sent to the second window.<br />

Listing 16-4: Checking Before Closing a Window<br />

<br />

<br />

window.closed Property<br />

<br />

// initialize global var for new window object<br />

// so it can be accessed by all functions on the page<br />

var newWind<br />

// set flag to help out with special handling for window closing<br />

var isIE3 = (navigator.appVersion.indexOf(“MSIE 3”) != -1) ? true : false<br />

// make the new window and put some stuff in it<br />

function newWindow() {<br />

newWind = window.open(“”,”subwindow”,”HEIGHT=200,WIDTH=200”)<br />

// take care of Navigator 2<br />

if (newWind.opener == null) {<br />

newWind.opener = window<br />

}<br />

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

}<br />

function finishNewWindow() {<br />

var output = “”<br />

output += “A Sub-window”<br />

output += “”

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

Saved successfully!

Ooh no, something went wrong!