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 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

is mySPAN) is, indeed, the source element of the event. Because event bubbling is<br />

enabled by default, the event bubbles upward to the SPAN element’s next outermost<br />

container: the myP paragraph element. (However, mySPAN is still the source element.)<br />

Finally, the event reaches the BODY element. If you click in the H1 element at the top<br />

of the page, the event is not processed until it reaches the BODY element — although<br />

the H1 element is the source element because that’s what you clicked. In all cases,<br />

when you explicitly click something to generate the onclick event, the event’s<br />

button property shows zero to signify the primary mouse button in IE.<br />

Now onto the real purpose of this example: the fireEvent() method. Three buttons<br />

enable you to direct a click event to each of the three elements that have event<br />

handlers defined for them. The events fired this way are artificial, generated via the<br />

createEventObject() method. For demonstration purposes, the button property<br />

of these scripted events is set to 3. This property value is assigned to the event<br />

object that eventually gets directed to an element. With event bubbling left on, the<br />

events sent via fireEvent() behave just like the physical clicks on the elements.<br />

Similarly, if you disable event bubbling, the first event handler to process the event<br />

cancels bubbling, and no further processing of that event occurs. Notice that event<br />

bubbling is cancelled within the event handlers that process the event. To prevent<br />

the clicks of the checkbox and action buttons from triggering the BODY element’s<br />

onClick event handlers, event bubbling is turned off for the buttons right away.<br />

Listing 15-26: Using the fireEvent() Method<br />

<br />

<br />

<br />

#mySPAN {font-style:italic}<br />

<br />

<br />

// assemble a couple event object properties<br />

function getEventProps() {<br />

var msg = “”<br />

var elem = event.srcElement<br />

msg += “event.srcElement.tagName: “ + elem.tagName + “\n”<br />

msg += “event.srcElement.id: “ + elem.id + “\n”<br />

msg += “event button: “ + event.button<br />

return msg<br />

}<br />

// onClick event handlers for body, myP, and mySPAN<br />

function bodyClick() {<br />

var msg = “Click event processed in BODY\n\n”<br />

msg += getEventProps()<br />

alert(msg)<br />

checkCancelBubble()<br />

}<br />

function pClick() {<br />

var msg = “Click event processed in P\n\n”<br />

msg += getEventProps()<br />

alert(msg)<br />

Continued<br />

67<br />

elementObject.fireEvent()

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

Saved successfully!

Ooh no, something went wrong!