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.

202<br />

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

popupObject.hide()<br />

If you then click into the main window to hide the pop-up, you will see a different<br />

result if you enter the following statement into the top text box by itself:<br />

alert(“Popup is open:” + a.isOpen)<br />

Methods<br />

hide()<br />

show(left, top, width, height[,<br />

positioningElementRef])<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓<br />

Example<br />

Listing 16-49 demonstrates both the show() and hide() methods for a popup<br />

object. A click of the button on the page invokes the selfTimer() function, which<br />

acts as the main routine for this page. The goal is to produce a pop-up window that<br />

“self-destructs” five seconds after it appears. Along the way, a message in the popup<br />

counts down the seconds.<br />

A reference to the pop-up window is preserved as a global variable, called<br />

popup. After the popup object is created, the initContent() function stuffs the<br />

content into the pop-up by way of assigning style properties and some innerHTML<br />

for the body of the document that is automatically created when the pop-up is generated.<br />

A SPAN element is defined so that another function later on can modify the<br />

content of just that segment of text in the pop-up. Notice that the assignment of<br />

content to the pop-up is predicated on the pop-up window having been initialized<br />

(by virtue of the popup variable having a value assigned to it) and that the pop-up<br />

window is not showing. While invoking initContent() under any other circumstances<br />

is probably impossible, the validation of the desired conditions is good programming<br />

practice.<br />

Back in selfTimer(), the popup object is displayed. Defining the desired size<br />

requires some trial and error to make sure the pop-up window comfortably accommodates<br />

the text that is put into the pop-up in the initContent() function.<br />

With the pop-up window showing, now is the time to invoke the countDown()<br />

function. Before the function performs any action, it validates that the pop-up has<br />

been initialized and is still visible. If a user clicks the main window while the<br />

counter is counting down, this changes the value of the isOpen property to false,<br />

and nothing inside the if condition executes.<br />

This countDown() function grabs the inner text of the SPAN and uses<br />

paresInt() to extract just the integer number (using base 10 numbering, because<br />

we’re dealing with zero-leading numbers that can potentially be regarded as octal<br />

values). The condition of the if construction decreases the retrieved integer by<br />

one. If the decremented value is zero, then the time is up, and the pop-up window is

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

Saved successfully!

Ooh no, something went wrong!