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.

onDblClick<br />

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

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

Compatibility ✓ ✓ ✓ ✓ ✓<br />

Example<br />

See Listing 15-35 (for the onClick event handler) to see the onDblClick event in<br />

action.<br />

onDrag<br />

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

Compatibility ✓ ✓<br />

Example<br />

Listing 15-37 shows several drag-related event handlers in action. The page<br />

resembles the example in Listing 15-36, but the scripting behind the page is quite<br />

different. In this example, the user is encouraged to select individual words from<br />

the Nouns and Adjectives columns and drag them to the blanks of the sentence. To<br />

beef up the demonstration, Listing 15-37 shows you how to pass the equivalent of<br />

array data from a drag source to a drag target. At the same time, the user has a<br />

fixed amount of time (two seconds) to complete each drag operation.<br />

The onDragStart and onDrag event handlers are placed in the tag<br />

because those events bubble up from any element that the user tries to drag. The<br />

scripts invoked by these event handlers filter the events so that the desired action<br />

is triggered only by the “hot” elements inside the table. This approach to event handlers<br />

prevents you from having to duplicate event handlers (or IE <br />

tags) for each table cell.<br />

The onDragStart event handler invokes setupDrag(). This function cancels the<br />

onDragStart event except when the target element (in other words, the one about<br />

to be dragged) is one of the TD elements inside the table. To make this application<br />

smarter about what kind of word is dragged to which blank, it passes not only the<br />

word’s text, but also some extra information about the word. This lets another event<br />

handler verify that a noun has been dragged to the first blank, while an adjective has<br />

been dragged to the second blank. To help with this effort, class names are assigned<br />

to the TD elements to distinguish the words from the Nouns column from the words<br />

of the Adjectives column. The setupDrag() function generates an array consisting of<br />

the innerText of the event’s source element plus the element’s class name. But the<br />

event.dataTransfer object cannot store array data types, so the Array.join()<br />

method converts the array to a string with a colon separating the entries. This string,<br />

then, is stuffed into the event.dataTransfer object. The object is instructed to render<br />

the cursor display during the drag-and-drop operation so that when the cursor is<br />

103<br />

elementObject.onDrag

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

Saved successfully!

Ooh no, something went wrong!