# Yahoo! UI Library

### Event Utility

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. There is not a capabilities check we can use here.

#### 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

#### 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 event bound to it.

#### 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 void getXY ( )`
Returns the pageX and pageY properties as an indexed array.

Returns: `void`

#### 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`

#### 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.

Returns: `void`

Copyright © 2006 Yahoo! Inc. All rights reserved.