The DOM Level 2 Event Model was designed with two main goals. The first goal was to offer a generic model that would allow the registration of event handlers, flow for events to follow, and basic contextual information regarding the event. The second goal was to provide backward compatibility with the event models of DOM Level 0 and Level 1.
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event
object.
The DocumentEvent
object provides a mechanism by which the user can create an Event
of a type supported by the implementation. It is expected that the DocumentEvent
interface will be implemented on the same object which implements the Document
interface in an implementation that supports the Event
model. Contents of the EventException
object are as follows:
Type | Item | Description |
Method |
| Creates a new event. |
Listing 12.1 shows the creation of a mouse event object using the createEvent()
of the DocumentEvent
object. The event is then initialized using the initEvent()
method.
<html>
<script language="JScript">
<!--
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initEvent("MouseEvent", true, false);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DocumentEvent.createEvent(eventType)
The createEvent()
method of the DocumentEvent
object creates a new event of the specified eventType.
Listing 12.2 shows the creation of a mouse event object using the CreateEvent()
of the DocumentEvent
object. The event is then initialized using the initEvent()
method.
<html>
<script language="JScript">
<!--
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initEvent("MouseEvent", true, false);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event object.
The Event
interface is used to provide contextual information about an event to the handler processing the event. An object that implements the Event
interface is generally passed as the first parameter to an event handler. More specific context information is passed to event handlers by deriving additional interfaces from Event
, which contain information directly relating to the type of event they accompany. The object passed to the event listener also implements these derived interfaces.
An Event
is created by using the createEvent()
method of the DocumentEvent
object. Table 12.1 lists all constants, methods and properties of the Event
object.
Listing 12.3 illustrates the creation of an event using the createEvent()
method of the DocumentEvent
object.
<html>
<script language="JScript">
<!--
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent();
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.type
The type
property of the Event
object identifies the name of the event.
Listing 12.4 illustrates the creation of an EventListener
function and checks the type
property of an Event
to see how it should be handled.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
if(evt.type. == "MouseEvent") {
processMouseEvent(evt);
} else if(evt.type == "MutationEvent") {
processMutationEvent(evt);
}
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.bubbles
The bubbles
property of the Event
object is of type boolean
and indicates whether this event can bubble.
Listing 12.5 illustrates the creation of an EventListener
function and checks the bubbles
property of the evt
parameter to determine whether the event bubbles.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
if(evt.bubbles)
handleBubblesEvent(evt);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.cancelable
Used to indicate whether an event can have its default action prevented. If the default action can be prevented, the value is true
; otherwise the value is false
.
Listing 12.6 illustrates the creation of an EventListener
function and checks the cancelable
property of the evt
parameter to determine whether the event’s default action can be canceled.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
if(evt.cancelable)
handleCancelableEvent(evt);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.currentTarget
The target
property of an Event
object indicates the EventTarget
object to which this event is currently being dispatched.
Listing 12.7 illustrates the creation of an EventListener
function and checks the currentTarget
property of the Event
object to see how the event should be handled.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
var currentTarget = evt.currentTarget;
if(currentTarget.type == "TextArea")
handleTextAreaEvent(target);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.eventPhase
Indicates which phase of the event flow is being evaluated: Event.CAPTURING_PHASE
, Event.AT_TARGET
, or Event.BUBBLING_PHASE
.
Listing 12.8 illustrates the creation of an EventListener
function and checks the eventPhase
property of the evt
parameter to determine at what phase the event is in.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
if(evt.eventPhase == Event.bubblingPhase)
processEventAtBubblingPhase(evt);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.initEvent(eventType, canBubble, cancelable)
The initEvent()
method of the Event
object is used to initialize the value of an Event
created using the createEvent()
of the DocumentEvent
object. This method can only be called before the Event
has been dispatched via the dispatchEvent()
method, although it can be called multiple times during that phase if necessary. If called multiple times, the final invocation takes precedence. If called from a subclass of the Event
interface, only the values specified in the initEvent()
method are modified: All other attributes are left unchanged. The eventType parameter is a string that specifies the event type,canBubble specifies whether this event can bubble, and cancelable specifies whether the event’s default action can be prevented.
Listing 12.9 shows the creation of a mouse event object using the CreateEvent()
of the DocumentEvent
object. The event is then initialized using the initEvent()
method.
<html>
<script language="JScript">
<!--
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initEvent("MouseEvent", true, false);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.preventDefault()
If an event is cancelable, the preventDefault()
method of the Event
object is used to signify that the event is to be canceled, meaning any default action normally taken by the implementation as a result of the event will not occur. If, during any stage of event flow, the preventDefault()
method is called, the event is canceled. Any default action associated with the event will not occur. Calling this method for a non-cancelable event has no effect. After preventDefault()
has been called, it will remain in effect throughout the remainder of the event’s propagation. This method can be used during any stage of event flow.
Listing 12.10 illustrates the prevention of the default behavior if an event is cancelable.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
if(evt.cancelable)
evt.preventDefault();
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.stopPropagation()
The stopPropagation()
method of the Event
object is used prevent further propagation of an event during event flow. If any EventListener
calls this method, the event will cease propagating through the tree. The event will complete dispatch to all listeners on the current EventTarget
before event flow stops. This method can be used during any stage of event flow.
Listing 12.11 illustrates the stoppage of the event flow of an event currently in the AT_TARGET
phase.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
if(evt.eventPhase = Event.AT_TARGET)
evt.stopPropagation();
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.target;
The target
property of the Event
object indicates the EventTarget
object to which this event was originally dispatched.
Listing 12.12 illustrates the creation of an EventListener
function and checks the target
property of the Event
object to see how the event should be handled.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
var target = evt.target;
if(target.type == "TextArea")
handleTextAreaEvent(target);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventObj.timeStamp
The timeStamp
property of the Event
object is used to specify the time (in milliseconds relative to the epoch) at which the event was created. Because some systems might not provide this information, the value of timeStamp
might not be available for all events. When not available, a value of 0
will be returned. Examples of epoch time are the time of the system start or 0:0:0 UTC 1st January 1970. The timeStamp
property is an object of type Date
.
Listing 12.13 illustrates the creation of an event handler that behaves differently depending on the time when the event originated.
<html>
<script language="JScript">
<!--
function GenericEventHandler(evt) {
if(evt.timeStamp < '11/24/2000')
handleTS(evt.timeStamp);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event
object.
The EventException
object is thrown when an exception condition is met while evaluating certain methods of other DOM Event
objects. Table 12.2 lists the constants and properties for the EventException
object.
Listing 12.14 illustrates the catching of an EventException
object and reading its code
property.
<html>
<script language="JScript">
<!--
function MouseEventHandler(evt) {
if(evt.type == "MouseEvent") {
if(evt.clientX ==5) && (evt.clientY ==5)
showDialog();
}
}
try {
var htmlDocument =
HTMLDOMImplementation.createHTMLDocument("My HTML Page");
var textArea = htmlDocument.createElement("TextArea");
textArea.addEventListener("MouseEvent", MouseEventHandler ,false);
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent();
textArea.dispatchEvent(mouseEvent);
}catch(evt) {
if(evt.code == EventException.UNSPECIFIED_EVENT_TYPE_ERR)
handleUnexpectedEvent(evt);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventExceptionObj.code
The code
property of the EventException
object contains the numeric code representing the actual exception.
Listing 12.15 illustrates the catching of an EventException
object and reading its code
property.
<html>
<script language="JScript">
<!--
function MouseEventHandler(evt) {
if(evt.type == "MouseEvent") {
if(evt.clientX ==5) && (evt.clientY ==5)
showDialog();
}
}
try {
var htmlDocument =
HTMLDOMImplementation.createHTMLDocument("My HTML Page");
var textArea = htmlDocument.createElement("TextArea");
textArea.addEventListener("MouseEvent", MouseEventHandler ,false);
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent();
textArea.dispatchEvent(mouseEvent);
}catch(evt) {
if(evt.code == EventException.UNSPECIFIED_EVENT_TYPE_ERR)
handleUnexpectedEvent(evt);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event
object.
The EventListener
is simply a function reference that takes an object of type Event
as an argument. When the event listener is registered, the body of the function will be executed when an specified type of event occurs.
Listing 12.16 shows the declaration of an EventListener
and then illustrates the registration of the listener with a TextArea
object.
<html>
<script language="JScript">
<!--
function MouseEventHandler(evt) {
if(evt.type == "MouseEvent") {
if(evt.clientX ==5) && (evt.clientY ==5)
showDialog();
}
}
var htmlDocument =
HTMLDOMImplementation.createHTMLDocument("My HTML Page");
var textArea = htmlDocument.createElement("TextArea");
textArea.addEventListener("MouseEvent", MouseEventHandler ,false);
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent();
textArea.dispatchEvent(mouseEvent);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event
object.
Specified by an Event
object’s target attribute, the EventTarget
represents the object that an event is directed to by the DOM implementation. When the event reaches the target, all appropriate EventListeners
are triggered.
All Node objects from a DOM implementation that supports the DOM Level 2 Event Model inherit from EventTarget
. Table 12.1 shows all methods available for the EventTarget
object. Table 12.3 lists all of the methods of the EventTarget
object.
Listing 12.17 demonstrates the assignment of an event target and fires off an event.
<html>
<script language="JScript">
<!--
function MouseEventHandler(evt) {
if(evt.type == "MouseEvent") {
if(evt.clientX ==5) && (evt.clientY ==5)
showDialog();
}
}
var htmlDocument = HTMLDOMImplementation.createHTMLDocument("My HTML Page");
var textArea = htmlDocument.createElement("TextArea");
textArea.addEventListener("MouseEvent", MouseEventHandler ,false);
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent();
textArea.dispatchEvent(mouseEvent);
// -->
</script>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
targetObj.addEventListener(type, listener, useCapture);
The addEventListener()
method of the EventTarget
object adds an event listener to this target. Type is the event type to be listened for, the listener parameter is the actual listener function call, and useCapture is a Boolean field that determines whether the event can be intercepted by an event listener found in an ancestor of the target.
Listing 12.18 demonstrates the assignment of an event listener and fires off an event.
<html>
<script language="JScript">
<!--
function MouseEventHandler(evt) {
if(evt.type == "MouseEvent") {
if(evt.clientX ==5) && (evt.clientY ==5)
showDialog();
}
}
var htmlDocument = HTMLDOMImplementation.createHTMLDocument("My HTML Page");
var textArea = htmlDocument.createElement("TextArea");
textArea.addEventListener("MouseEvent", MouseEventHandler ,false);
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent();
textArea.dispatchEvent(mouseEvent);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventTargetObj.dispatchEvent(evt);
The dispatchEvent()
method of the EventTarget
object directs the event evt to this EventTarget
object.
Listing 12.19 demonstrates using the dispatchEvent()
method to direct a mouse event to an HTML TextArea
object.
<html>
<script language="JScript">
<!--
function MouseEventHandler(evt) {
if(evt.type == "MouseEvent") {
if(evt.clientX ==5) && (evt.clientY ==5)
showDialog();
}
}
var htmlDocument = HTMLDOMImplementation.createHTMLDocument("My HTML Page");
var textArea = htmlDocument.createElement("TextArea");
textArea.addEventListener("MouseEvent", MouseEventHandler ,false);
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent();
textArea.dispatchEvent(mouseEvent);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
eventListenerObj.removeListener(type, listener, useCapture)
The removeEventListener()
method of the EventTarget
object removes the described and referenced listener from the list of event listeners maintained by this event target. type is the event type to being listened for, the listener parameter is the actual listener function call, and useCapture is a Boolean field that determines whether the event can be intercepted by an event listener found in an ancestor of the target.
Listing 12.20 demonstrates removing an event listener from the first TextArea
object that appears in an HTML document.
<html>
<script language="JScript">
<!--
function MouseEventHandler(evt) {
if(evt.type == "MouseEvent") {
if(evt.clientX ==5) && (evt.clientY ==5)
showDialog();
}
}
var htmlDocument = HTMLDOMImplementation.createHTMLDocument("My HTML Page");
var textAreaList = htmlDocument.getElementsByTagName("TextArea");
var firstTextArea = textAreaList.index(0);
firstTextArea.removeEventListener("MouseEvent", MouseEventHandler, false);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event
object.
The MouseEvent
object provides specific contextual information associated with mouse events. The MouseEvent
object inherits all properties and methods from the UIMethod
object. Table 12.4 lists all of the properties and methods of the MouseEvent
object.
Listing 12.21 shows the creation of a mouse event object using the createEvent()
method of the DocumentEvent
object. The event is then initialized using the initEvent()
method.
<html>
<script language="JScript">
<!--
var mouseEvent = DocumentEvent.createEvent("MouseEvent");
mouseEvent.initMouseEvent("click", true, false, );
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.altKey
Used to indicate whether the Alt key was depressed during the firing of the event. On some platforms, this key might map to an alternative key name.
Listing 12.22 illustrates an event listener method as well as inspecting the altKey
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if((evt.type == "click") && (evt.altKey))
processClickAndCTRL(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.Button
During mouse events caused by the depression or release of a mouse button, button
is used to indicate which mouse button changed state. The values for button
range from 0
to indicate the left button of the mouse, 1
to indicate the middle button if present, and 2
to indicate the right button. In mice configured for left-handed use in which the button actions are reversed, the values are instead read from right to left.
Listing 12.23 illustrates an event listener method as well as inspecting which mouse button was pressed if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if((evt.type == "click") && (evt.button == 0))
processLeftMouseClick(evt);
if((evt.type == "click") && (evt.button == 1))
processMiddleMouseClick(evt);
if((evt.type == "click") && (evt.button == 2))
processRightMouseClick(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.clientX
The clientX
property of the MouseEvent
object represents the horizontal coordinate at which the event occurred relative to the DOM implementation’s client area.
Listing 12.24 illustrates an event listener method as well as inspecting the clientX
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if(evt.type == "click")
processXLocation(evt.clientX);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.clientY
The clientY
property of the MouseEvent
object represents the vertical coordinate at which the event occurred relative to the DOM implementation’s client area.
Listing 12.25 illustrates an event listener method as well as inspecting the clientY
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if(evt.type == "click")
processXLocation(evt.clientY);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.ctrlKey
Used to indicate whether the CTRL key was depressed during the firing of the event.
Listing 12.26 illustrates an event listener method as well as inspecting the ctrlKey
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if((evt.type == "click") && (evt.ctrlKey))
processClickAndCTRL(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript
5.0+ Nav6+, IE5+
mouseEventObj.initMouseEvent(type, canBubble, cancelable, view, detail,
screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey,
button, relatedTarget)
The initMouseEvent()
method of the MouseEvent
object is used to initialize the value of a MouseEvent
created through the DocumentEvent
object. This method can only be called before the MouseEvent
has been dispatched via the dispatchEvent()
method, although it can be called multiple times during that phase if necessary. If called multiple times, the final invocation takes precedence.
The following types of mouse events are supported by DOM Level 2: click, mouseup, mousedown, mouseover, mousemove, and mouseout.
Listing 12.27 shows the creation of a mouse event object using the createEvent()
of the DocumentEvent
object. The event is then initialized using the initEvent()
method to initialize it.
<html>
<script language="JScript">
<!--
var mouseEvent = DocumentEvent.CreateEvent("MouseEvent");
mouseEvent.initMouseEvent("click", true, false, );
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.metaKey
The metaKey
property of the MouseEvent
object is used to indicate whether the META key was depressed during the firing of the event. On some platforms, this key might map to an alternative key name.
Listing 12.28 illustrates an event listener method as well as inspecting the metaKey
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if((evt.type == "click") && (evt.metaKey))
processClickAndCTRL(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEvent.relatedTarget
The relatedTarget
property of the MouseEvent
object is used to identify a secondary EventTarget
related to a UI event. Currently, this attribute is used with the mouseover event to indicate the EventTarget
, which the pointing device exited, and with the mouseout event to indicate the EventTarget
, which the pointing device entered.
Listing 12.29 illustrates reading the relatedTarget
property of the MouseEvent
object.
<html>
<script language="JScript">
<!--function handleMouseEvent(evt) {
if(evt.relatedTarget == otherTarget
processSecondaryTarget(otherTarget);
}
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.screenX
The screenX
property of the MouseEvent
object represents the horizontal coordinate at which the event occurred relative to the origin of the screen coordinate system.
Listing 12.30 illustrates an event listener method as well as inspecting the screenX
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if(evt.type == "click")
processXLocation(evt.ScreenX);
}
// --> </script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.screenY
The screenY
property of the MouseEvent
object represents the vertical coordinate at which the event occurred relative to the origin of the screen coordinate system.
Listing 12.31 illustrates an event listener method as well as inspecting the screenY
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if(evt.type == "click")
processXLocation(evt.screenY);
}
// --> </script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mouseEventObj.shiftKey
The shiftKey
property of the MouseEvent
object is used to indicate whether the Shift key was depressed during the firing of the event.
Listing 12.32 illustrates an event listener method as well as inspecting the shiftKey
property if the event is a "click"
mouse event.
<html>
<script language="JScript">
<!--
function handleMouseEvent(evt) {
if((evt.type == "click") && (evt.shiftKey))
processClickAndSHIFT(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event
object.
The MutationEvent
object is designed to allow notification of any changes to the structure of a document, including Attr
and Text
modifications. It might be noted that none of the mutation events listed are designated as cancelable. This stems from the fact that it is very difficult to make use of existing DOM objects that cause document modifications if any change to the document might or might not take place because of the cancellation of the related event. Although this is still a desired capability, it was decided that it would be better left until the addition of transactions into the DOM. Many single modifications of the tree can cause multiple mutation events to be fired. Rather than attempt to specify the ordering of mutation events because of every possible modification of the tree, the ordering of these events is left to the implementation.
A DOM application can use the hasFeature()
method of the DOMImplementation
object with parameter values MutationEvents
and 2.0
(respectively) to determine whether the mutation event module is supported by the implementation. Table 12.5 lists all of the constants, properties, and methods of the MutationEvent
object.
Listing 12.33 shows the creation of a mutation event object using the createEvent()
of the DocumentEvent
object. The event is then initialized using the initEvent()
method to initialize it.
<html>
<script language="JScript">
<!--
var mutationEvent = DocumentEvent.createEvent("MutationEvent");
mutationEvent.initEvent("DOMNodeInserted", true, false, relatedNode,
prevValue, newValue, attrName, attrChange);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mutationEventObj.relatedNode
The relatedNode
property of the MutationEvent
object is used to identify a secondary node related to a mutation event. For example, if a mutation event is dispatched to a node indicating that its parent has changed, the relatedNode
is the changed parent. If an event is instead dispatched to a subtree indicating a node was changed within it, the relatedNode
is the changed node. In the case of the DOMAttrModified
event it indicates the Attr
node which was modified, added, or removed.
Listing 12.34 shows the creation of a mutation event object using the createEvent()
of the DocumentEvent
object. The event is then initialized using the initEvent()
method to initialize it.
<html>
<script language="JScript">
<!--
var mutationEvent = DocumentEvent.createEvent("MutationEvent");
mutationEvent.initEvent("DOMNodeInserted", true, false, relatedNode,
prevValue, newValue, attrName, attrChange);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mutationEvent.attrChange
The attrChange
property of the MutationEvent
object indicates the type of change that triggered the DOMAttrModified
event. The values can be MODIFICATION
, ADDITION
, or REMOVAL
.
Listing 12.35 checks the attrChange
property to determine how an attribute was modified and handles the event accordingly.
<html>
<script language="JScript">
<!--
function handleMutation(evt) {
if(evt.attrChange == MutationEvent.ADDITION)
handleTextAreaAddition(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mutationEvent.attrName
The attrName
property of the MutationEvent
object indicates the name of the changed Attr
node in a DOMAttrModified
event.
Listing 12.36 checks the attrName
property to determine if the modified attribute is a TextArea
. If so, the event is handled appropriately.
<html>
<script language="JScript">
<!--
function handleMutation(evt) {
if(evt.attrName == "TextArea")
handleTextAreaModification(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mutationEventObj.initMutationEvent(type, canBubble, cancelable,
relatedNode, prevValue, newValue, attrName, attrChange)
The initMutationEvent()
method is used to initialize the value of a MutationEvent
created using the DocumentEvent
object. This method can only be called before the MutationEvent
has been dispatched via the dispatchEvent()
method, though it can be called multiple times during that phase if necessary. If called multiple times, the final invocation takes precedence.
The possible values for type
are DOMSubTreeModified
, DOMNodeInserted
, and DOMNodeRemoved
.
Listing 12.37 shows the creation of a mutation event object using the createEvent()
of the DocumentEvent
object. The event is then initialized using the initEvent()
method to initialize it.
<html>
<script language="JScript">
<!--
var mutationEvent = DocumentEvent.createEvent("MutationEvent");
mutationEvent.initMutationEvent
("DOMNodeInserted", true, false, relatedNode,
prevValue, newValue, attrName, attrChange);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mutationEventObj.newValue
The newValue
property of the MutationEvent
object indicates the new value of the Attr
node in DOMAttrModified
events and of the CharacterData
node in DOMCharDataModified
events.
Listing 12.38 checks the prevValue
and newValue
properties to determine whether any actual changes have occurred to the Attr
in question.
<html>
<script language="JScript">
<!--
function handleMutation(evt) {
if(evt.prevValue == evt.newValue)
handleUnchangedEvent(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
mutationEventObj.prevValue
The prevValue
property indicates the previous value of the Attr
node in DOMAttrModified
events, and of the CharacterData
node in DOMCharDataModified
events.
Listing 12.39 checks the prevValue
and newValue
properties to determine whether any actual changes have occurred to the Attr
in question.
<html>
<script language="JScript">
<!--
function handleMutation(evt) {
if(evt.prevValue == evt.newValue)
handleUnchangedEvent(evt);
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
DOM Event
object.
The UIEvent
object provides specific contextual information associated with User Interface events. The UIEvent
object inherits all properties and methods from the Event
object. Table 12.6 lists the properties and methods of the UIEvent
object.
Listing 12.40 shows the creation of a user interface event object using the createEvent()
method of the DocumentEvent
object. The event is then initialized using the initUIEvent()
method.
<html>
<script language="JScript">
<!--
var uiEvent = DocumentEvent.createEvent("UIEvent");
uiEvent.initUIEvent("DOMFocusOut", true, false, );
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
uiEventObj.detail
The detail
property of the UIEvent
object specifies some detail information about the Event
, depending on the type of event.
Listing 12.41 shows the creation of a user interface event object using the createEvent()
method of the DocumentEvent
object. The event is then initialized using the initEvent()
method. After initialization, the detail
property is written to.
<html>
<script language="JScript">
<!--
var uiEvent = DocumentEvent.createEvent("UIEvent");
uiEvent.initUIEvent("DOMFocusOut", true, false, );
uiEvent.detail =
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
uiEventObj.initUIEvent(type , canBubble, cancelable, view, detail)
The initUIEvent()
method of the UIEvent
object is used to initialize the value of an UIEvent
created through the DocumentEvent
object. This method can only be called before the UIEvent
has been dispatched via the dispatchEvent()
method, although it can be called multiple times during that phase if necessary. If called multiple times, the final invocation takes precedence.
The type parameter
is a string that can be DOMFocusIn, DOMFocusOut, or DOMActivate. The canBubble parameter indicates whether this event can bubble, the cancelable parameter indicates whether this event can be canceled,view is the AbstractView
object in which this UIEvent
originated, and detail is a number that represents some type of detailed information regarding an UIEvent
of the specified type.
Listing 12.42 shows the creation and initialization of a UIEvent using the initUIEvent()
method.
<html>
<script language="JScript">
<!--
var mouseEvent = DocumentEvent.createEvent("UIEvent");
mouseEvent.initUIEvent("DOMFocusOut", true, false, );
// -->
</script>
</html>
JavaScript 1.5+, JScript 5.0+
Nav6+, IE5+
uiEventObj.view
The view
property of the UIEvent
object identifies the AbstractView
from which the event was generated.
Listing 12.43 show the writing of the view
property of the UIEvent
object.
<html>
<script language="JScript">
<!--
var uiEvent = DocumentEvent.CreateEvent("UIEvent");
uiEvent.InitUIEvent("DOMFocusOut", true, false, );
uiEvent.view = "display";
// -->
</script>
</html>
3.145.59.187