283Chapter 15Generic HTML Element Objectsexisting eventobject is to (Web site hosting)

283Chapter 15Generic HTML Element Objectsexisting eventobject is to set the properties of the eventobject that the fireEvent()methodcreates. The event type is defined by the method s first parameter, but if you have other proper- ties to set (for example, coordinates or a keyboard key code), then those properties are pickedup from the existing object. Here is an example of a sequence that creates a new mousedownevent, stuffs some values into its properties, and then fires the event at an element on the page: var newEvent = document.createEventObject(); newEvent.clientX = 100; newEvent.clientY = 30; newEvent.cancelBubble = false; newEvent.button = 1; document.getElementById( myElement ).fireEvent( onmousedown , newEvent); Events generated by the fireEvent()method are just like regular IE window.eventobjects, and they have several important eventobject properties that the browser presets. Importantly, cancelBubbleis set to falseand returnValueis set to true just like a regular user- or system-induced event. This means that if you want to prevent event bubbling and/or preventthe default action of the event s source element, then the event handler functions must setthese eventobject properties just like normal event handling in IE. The fireEvent()method returns a Boolean value that the returnValueproperty of theevent determines. If the returnValueproperty is set to falseduring event handling, thefireEvent()method returns false. Under normal processing, the method returns true. Although the W3C DOM Level 2 event model includes the dispatchEvent()method toaccommodate script-generated events (and Eventobject methods to create event objects), Microsoft has so far elected to ignore the standard recommendation. While there is some sim- ilarity between the basic operations of fireEvent()and dispatchEvent(), the two meth- ods diverge significantly in advanced applications (for example, the way events canpropagate and the W3C notion of an Eventobject). ExampleListing 15-26 contains script code that shows how to programmatically fire events using thefireEvent()method. Three buttons in the example page enable you to direct a click event toeach of the three elements that have event handlers defined for them. The events fired this wayare artificial, generated via the createEventObject()method. For demonstration purposes, the buttonproperty of these scripted events is set to 3. This property value is assigned to theeventobject that eventually gets directed to an element. With event bubbling left on, theevents sent via fireEvent()behave just like the physical clicks on the elements. Similarly, ifyou disable event bubbling, the first event handler to process the event cancels bubbling, andno further processing of that event occurs. Notice that event bubbling is cancelled within theevent handlers that process the event. To prevent the clicks of the checkbox and action buttonsfrom triggering the bodyelement s onclickevent handlers, event bubbling is turned off for thebuttons right away. Listing 15-26: Using the fireEvent() Method ContinuedelementObject.fireEvent()

Leave a Reply