YUI recommends YUI 3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

Yahoo! UI Library

Event Utility  2.2.0

Yahoo! UI Library > event > YAHOO.util.Event

static Class YAHOO.util.Event

The event utility provides functions to add and remove event listeners, event cleansing. It also tries to automatically remove listeners it registers during the unload event.

Properties

_interval - private object

poll handle

ADJ_SCOPE - static final int

Adjusted scope, either the element we are registering the event on or the custom object passed in by the listener, int constant

counter - private static object

Counter for auto id generation

EL - static final int

Element to bind, int constant

elCache DOM element cache - private static object


FN - static final int

Function to execute, int constant

isIE - private static object

IE detection needed to properly calculate pageX and pageY. capabilities checking didn't seem to work because another browser that does not provide the properties have the values calculated in a different manner than IE.

isSafari - private static object

Safari detection is necessary to work around the preventDefault bug that makes it so you can't cancel a href click from the handler. Since this function has been used outside of this utility, it was changed to detect all KHTML browser to be more friendly towards the non-Safari browsers that share the engine. Internally, the preventDefault bug detection now uses the webkit property.
Deprecated:  

lastError - Error

addListener/removeListener can throw errors in unexpected scenarios. These errors are suppressed, the method returns false, and this property is set

legacyEvents - private static object

Cache of DOM0 event handlers to work around issues with DOM2 events in Safari

legacyHandlers - private static object

Listener stack for DOM0 events

legacyMap - private static object

Lookup table for legacy events

listeners - private static array

Cache of wrapped listeners

loadComplete - private static boolean

True after the onload event has fired

OBJ - static final int

Object passed in by the user that will be returned as a parameter to the callback, int constant

onAvailStack - private static object

onAvailable listeners

POLL_INTERVAL - static final int

The poll interval in milliseconds

POLL_RETRYS - static final int

The number of times we should look for elements that are not in the DOM at the time the event is requested after the document has been loaded. The default is 200@amp;50 ms, so it will poll for 10 seconds or until all outstanding handlers are bound (whichever comes first).

retryCount - private static object

The number of times to poll after window.onload. This number is increased if additional late-bound handlers are requested after the page load.

TYPE - static final int

Type of event, int constant

unloadListeners - private static array

User-defined unload function that will be fired before all events are detached

webkit - object

If WebKit is detected, we keep track of the version number of the engine. Safari 1.3.2 (312.6): 312.8.1 <-- currently the latest available on Mac OSX 10.3. Safari 2.0.2: 416 <-- hasOwnProperty introduced Safari 2.0.4: 418 <-- preventDefault fixed (I believe) Safari 2.0.4 (419.3): 418.9.1 <-- current release http://developer.apple.com/internet/safari/uamatrix.html

WFN - static final int

Function wrapped for scope correction and cleanup, int constant

Methods

_getCacheIndex

private static void _getCacheIndex ( )
Locating the saved event handler data by function ref
Returns: void

_getScroll

private static void _getScroll ( )
Returns the scrollTop and scrollLeft. Used to calculate the pageX and pageY in Internet Explorer
Returns: void

_getScrollLeft

private static void _getScrollLeft ( )
Returns scrollLeft
Returns: void

_getScrollTop

private static void _getScrollTop ( )
Returns scrollTop
Returns: void

_isValidCollection

private static boolean _isValidCollection ( o )
We want to be able to use getElementsByTagName as a collection to attach a group of events to. Unfortunately, different browsers return different types of collections. This function tests to determine if the object is array-like. It will also fail if the object is an array, but is empty.
Parameters:
o <object> the object to test
Returns: boolean
true if the object is array-like and populated

_load

private static void _load ( )
hook up any deferred listeners
Returns: void

_simpleAdd

private static void _simpleAdd ( el , sType , fn , capture )
Adds a DOM event directly without the caching, cleanup, scope adj, etc
Parameters:
el <HTMLElement> the element to bind the handler to
sType <string> the type of event handler
fn <function> the callback to invoke
capture <boolen> capture or bubble phase
Returns: void

_simpleRemove

private static void _simpleRemove ( el , sType , fn , capture )
Basic remove listener
Parameters:
el <HTMLElement> the element to bind the handler to
sType <string> the type of event handler
fn <function> the callback to invoke
capture <boolen> capture or bubble phase
Returns: void

_tryPreloadAttach

private static void _tryPreloadAttach ( )
Polling function that runs before the onload event fires, attempting to attach to DOM Nodes as soon as they are available
Returns: void

_unload

private static void _unload ( )
Removes all listeners registered by pe.event. Called automatically during the unload event.
Returns: void

addListener

static boolean addListener ( el , sType , fn , obj , override )
Appends an event handler
Parameters:
el <Object> The html element to assign the event to
sType <String> The type of event to append
fn <Function> The method the event invokes
obj <Object> An arbitrary object that will be passed as a parameter to the handler
override <boolean> If true, the obj passed in becomes the execution scope of the listener
Returns: boolean
True if the action was successful or defered, false if one or more of the elements could not have the listener attached, or if the operation throws an exception.

clearCache

private static void clearCache ( )
Clears the element cache
Returns: void
Deprecated Elements are not cached any longer

fireLegacyEvent

private static void fireLegacyEvent ( )
When using legacy events, the handler is routed to this object so we can fire our custom listener stack.
Returns: void

generateId

static string generateId ( el )
Generates an unique ID for the element if it does not already have one.
Parameters:
el <object> the element to create the id for
Returns: string
the resulting id of the element

getCharCode

static int getCharCode ( ev )
Returns the charcode for an event
Parameters:
ev <Event> the event
Returns: int
the event's charCode

getEl

private static void getEl ( )
We cache elements bound by id because when the unload event fires, we can no longer use document.getElementById
Returns: void

getEvent

static Event getEvent ( e )
Finds the event in the window object, the caller's arguments, or in the arguments of another method in the callstack. This is executed automatically for events registered through the event manager, so the implementer should not normally need to execute this function at all.
Parameters:
e <Event> the event parameter from the handler
Returns: Event
the event

getLegacyIndex

private static void getLegacyIndex ( )
Returns the legacy event index that matches the supplied signature
Returns: void

getListeners

static Object getListeners ( el , sType )
Returns all listeners attached to the given element via addListener. Optionally, you can specify a specific type of event to return.
Parameters:
el <HTMLElement> the element to inspect
sType <string> optional type of listener to return. If left out, all listeners will be returned
Returns: Object
the listener. Contains the following fields:   type: (string) the type of event   fn: (function) the callback supplied to addListener   obj: (object) the custom object supplied to addListener   adjust: (boolean) whether or not to adjust the default scope   index: (int) its position in the Event util listener cache

getPageX

static int getPageX ( ev )
Returns the event's pageX
Parameters:
ev <Event> the event
Returns: int
the event's pageX

getPageY

static int getPageY ( ev )
Returns the event's pageY
Parameters:
ev <Event> the event
Returns: int
the event's pageY

getRelatedTarget

static HTMLElement getRelatedTarget ( ev )
Returns the event's related target
Parameters:
ev <Event> the event
Returns: HTMLElement
the event's relatedTarget

getTarget

static HTMLElement getTarget ( ev , resolveTextNode )
Returns the event's target element
Parameters:
ev <Event> the event
resolveTextNode <boolean> when set to true the target's parent will be returned if the target is a text node. @deprecated, the text node is now resolved automatically
Returns: HTMLElement
the event's target

getTime

static Date getTime ( ev )
Returns the time of the event. If the time is not included, the event is modified using the current time.
Parameters:
ev <Event> the event
Returns: Date
the time of the event

getXY

static [x, y] getXY ( ev )
Returns the pageX and pageY properties as an indexed array.
Parameters:
ev <Event> the event
Returns: [x, y]
the pageX and pageY properties of the event

on

static void on ( )
YAHOO.util.Event.on is an alias for addListener
Returns: void

onAvailable

static void onAvailable ( p_id , p_fn , p_obj , p_override )
Executes the supplied callback when the item with the supplied id is found. This is meant to be used to execute behavior as soon as possible as the page loads. If you use this after the initial page load it will poll for a fixed time for the element. The number of times it will poll and the frequency are configurable. By default it will poll for 10 seconds.
Parameters:
p_id <string> the id of the element to look for.
p_fn <function> what to execute when the element is found.
p_obj <object> an optional object to be passed back as a parameter to p_fn.
p_override <boolean> If set to true, p_fn will execute in the scope of p_obj
Returns: void

onContentReady

static void onContentReady ( p_id , p_fn , p_obj , p_override )
Works the same way as onAvailable, but additionally checks the state of sibling elements to determine if the content of the available element is safe to modify.
Parameters:
p_id <string> the id of the element to look for.
p_fn <function> what to execute when the element is ready.
p_obj <object> an optional object to be passed back as a parameter to p_fn.
p_override <boolean> If set to true, p_fn will execute in the scope of p_obj
Returns: void

preventDefault

static void preventDefault ( ev )
Prevents the default behavior of the event
Parameters:
ev <Event> the event
Returns: void

purgeElement

static void purgeElement ( el , recurse , sType )
Removes all listeners attached to the given element via addListener. Optionally, the node's children can also be purged. Optionally, you can specify a specific type of event to remove.
Parameters:
el <HTMLElement> the element to purge
recurse <boolean> recursively purge this element's children as well. Use with caution.
sType <string> optional type of listener to purge. If left out, all listeners will be removed
Returns: void

regCE

private void regCE ( )
Used by old versions of CustomEvent, restored for backwards compatibility
Returns: void

removeListener

static boolean removeListener ( el , sType , fn )
Removes an event handler
Parameters:
el <Object> the html element or the id of the element to assign the event to.
sType <String> the type of event to remove.
fn <Function> the method the event invokes. If fn is undefined, then all event handlers for the type of event are removed.
Returns: boolean
true if the unbind was successful, false otherwise.

resolveTextNode

static HTMLElement resolveTextNode ( node )
In some cases, some browsers will return a text node inside the actual element that was targeted. This normalizes the return value for getTarget and getRelatedTarget.
Parameters:
node <HTMLElement> node to resolve
Returns: HTMLElement
the normized node

startInterval

private static void startInterval ( )
Returns: void

stopEvent

static void stopEvent ( ev )
Convenience method for stopPropagation + preventDefault
Parameters:
ev <Event> the event
Returns: void

stopPropagation

static void stopPropagation ( ev )
Stops event propagation
Parameters:
ev <Event> the event
Returns: void

useLegacyEvent

private static void useLegacyEvent ( )
Logic that determines when we should automatically use legacy events instead of DOM2 events. Currently this is limited to old Safari browsers with a broken preventDefault
Returns: void


Copyright © 2007 Yahoo! Inc. All rights reserved.