04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The Internet Explorer Event Object<br />

Chapter 12: Events<br />

Unlike the DOM event object, the IE event object is accessible in different ways based on the way in<br />

which the event handler was assigned. When an event handler is assigned using the DOM Level 0<br />

approach, the event object exists only as a property of the window object. Here is an example:<br />

var btn = document.getElementById(“myBtn”);<br />

btn.onclick = function(){<br />

var event = window.event;<br />

alert(event.type); //”click”<br />

};<br />

Here, the event object is retrieved from window.event and then used to determine the type of event<br />

that was fired (the type property for IE is identical to that of the DOM version). However, if the event<br />

handler is assigned using attachEvent(), the event object is passed in as the sole argument to the<br />

function as shown here:<br />

var btn = document.getElementById(“myBtn”);<br />

btn.attachEvent(“onclick”, function(event){<br />

alert(event.type); //”click”<br />

});<br />

When using attachEvent() , the event object is also available on the window object, as with the DOM<br />

Level 0 approach. It is also passed in as an argument for convenience.<br />

If the event handler is assigned by an HTML attribute, the event object is available as a variable called<br />

event (the same as the DOM model). Here’s an example:<br />

< input type=”button” value=”Click Me” onclick=”alert(event.type)” / ><br />

The IE event object also contains properties and methods related to the specific event that caused its<br />

creation. Many of these either map directly to or are related to DOM properties or methods. Like the<br />

DOM event object, the available properties and methods differ based on the type of event that was<br />

fired, but all events use the properties and methods defined in the following table.<br />

Property/Method Type Read/Write Description<br />

cancelBubble Boolean Read/Write False by default, but can be set to true to cancel<br />

event bubbling (same as the DOM<br />

stopPropagation() method)<br />

returnValue Boolean Read/Write True by default, but can be set to false to cancel the<br />

default behavior of the event (same as the DOM<br />

preventDefault() method)<br />

srcElement Element Read only The target of the event (same the DOM target<br />

property)<br />

type String Read only The type of event that was fired<br />

379

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

Saved successfully!

Ooh no, something went wrong!