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.

Chapter 2 ✦ Window and Frame Objects (Chapter 16)<br />

The window.open() method doesn’t provide parameters for setting the new<br />

window’s background color, so I let the getColor() function in the new window do<br />

the job as the document loads. The function uses the opener property to find out<br />

which radio button on the main page is selected.<br />

Listing 16-12: References to the opener Property<br />

<br />

<br />

New Window on the Block<br />

<br />

function getColor() {<br />

// shorten the reference<br />

colorButtons = self.opener.document.forms[0].color<br />

// see which radio button is checked<br />

for (var i = 0; i < colorButtons.length; i++) {<br />

if (colorButtons[i].checked) {<br />

return colorButtons[i].value<br />

}<br />

}<br />

return “white”<br />

}<br />

<br />

<br />

<br />

document.write(“”)<br />

<br />

This is a new window.<br />

<br />

<br />

Type text here for the main window:<br />

<br />

<br />

<br />

<br />

In the getColor() function, the multiple references to the radio button array<br />

can be very long. To simplify the references, the getColor() function starts out by<br />

assigning the radio button array to a variable I arbitrarily call colorButtons. That<br />

shorthand now stands in for lengthy references as I loop through the radio buttons<br />

to determine which button is checked and retrieve its value property.<br />

A button in the second window simply fetches the title of the opener window’s<br />

document. Even if another document loads in the main window in the meantime,<br />

the opener reference still points to the main window: Its document object, however,<br />

will change.<br />

143<br />

windowObject.opener

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

Saved successfully!

Ooh no, something went wrong!