04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12: Events<br />

(continued)<br />

event.view = document.defaultView;<br />

event.detail = 0;<br />

event.screenX = 0;<br />

event.screenY = 0;<br />

event.clientX = 0;<br />

event.clientY = 0;<br />

event.ctrlKey = false;<br />

event.altKey = false;<br />

event.metaKey = false;<br />

event.shiftKey = false;<br />

event.button = 0;<br />

event.relatedTarget = null;<br />

//fire the event<br />

btn.dispatchEvent(event);<br />

This code properly simulates mouse events for Safari 2.x and should only be used if event simulation<br />

in this browser if necessary.<br />

Firefox prior to version 3.0 did not allow assignment of the relatedTarget<br />

argument. Any value passed in was ignored, and the relatedTarget property on<br />

the event object always ended up as null.<br />

Simulating Keyboard Events<br />

As mentioned previously, keyboard events were left out of DOM Level 2 Events, so simulating keyboard<br />

events is not straightforward. Keyboard events were included in draft versions of DOM Level 2 Events<br />

and were removed before finalization. Firefox implements the draft version of keyboard events. It ’ s<br />

worth noting that keyboard events in DOM Level 3 are drastically different from the draft version<br />

originally included in DOM Level 2.<br />

Firefox allows you to create a keyboard event by passing “ KeyEvents ” into the createEvent()<br />

method. This returns an event object with a method called initKeyEvent() , which accepts the<br />

following 10 arguments:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

type (string) — The type of event to fire, such as “ click ”.<br />

bubbles (Boolean) — Indicates if the event should bubble. This should be set to true for<br />

accurate mouse event simulation.<br />

cancelable (Boolean) — Indicates if the event can be canceled. This should be set to true for<br />

accurate mouse event simulation.<br />

view (AbstractView) — The view associated with the event. This is almost always document<br />

.defaultView .<br />

ctrlKey (Boolean) — Indicates if the Ctrl key is pressed. The default is false .<br />

altKey (Boolean) — Indicates if the Alt key is pressed. The default is false .<br />

428

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

Saved successfully!

Ooh no, something went wrong!