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.

Chapter 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

The quote marks and parentheses can get jumbled easily, so enter this expression<br />

carefully. When you enter the expression successfully, the Results box shows<br />

the function’s text. Now assign this function to the onmousedown event of the myP<br />

element by entering the following statement into the upper text box:<br />

document.all.myP.attachEvent(“onmousedown”, a)<br />

The Results box displays true when successful. If you mouse down on the first<br />

paragraph, an alert box displays the date and time that the anonymous function<br />

was created (when the new Date() expression was evaluated).<br />

Now, disconnect the event relationship from the object by entering the following<br />

statement into the upper text box:<br />

document.all.myP.detachEvent(“onmousedown”, a)<br />

blur()<br />

focus()<br />

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

Compatibility ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

To show how both the window.focus() method and its opposite<br />

(window.blur()) operate, Listing 15-23 for NN3+ and IE4+ creates a two-window<br />

environment. From each window, you can bring the other window to the front. The<br />

main window uses the object returned by window.open() to assemble the reference<br />

to the new window. In the subwindow (whose content is created entirely on<br />

the fly by <strong>JavaScript</strong>), self.opener is summoned to refer to the original window,<br />

while self.blur() operates on the subwindow itself (except for the buggy behavior<br />

of NN6 noted earlier). Blurring one window and focusing on another window<br />

yields the same result of sending the window to the back of the pile.<br />

Listing 15-23: The window.focus() and window.blur()<br />

Methods<br />

<br />

<br />

Window Focus() and Blur()<br />

<br />

// declare global variable name<br />

var newWindow = null<br />

function makeNewWindow() {<br />

// check if window already exists<br />

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

// store new window object in global variable<br />

Continued<br />

59<br />

elementObject.blur()

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

Saved successfully!

Ooh no, something went wrong!