JavaScript » Objects » Event

An Event object is created automatically by JavaScript on the occurance of an event. It has various properties that provide information about the event such as event type, the position of the cursor at the time the event occured, etc. Not all of the properties relate to every type of event; the one's that do are documented in the individual event handler pages.

Examples

Code:
<input type="button" value="Event type" onClick='alert("The event type is " + event.type)'>
Explanation:

This example creates a button that, when clicked, displays an alert box showing the event type (in this case a 'click' event).

Properties

constructor

Syntax: Object.constructor

This specifies a function to create an object's property and is inherited by all objects from their prototype.

data

Syntax: event.data

This property relates to the DragDrop event and its use returns an array that contains the URLs of any dropped objects, as strings.

height

Syntax: event.height

This property relates to the height of the window or frame that contains the object that initiated the event.

keyCode

Syntax: event.keyCode

Contains the last key that was pressed.

layerX

Syntax: event.layerX

These properties returns a number that represents the horizontal/vertical position, in pixels, of the cursor relative to the layer that initiated the event, or, when passed with a resize event, it represents the object width/height. These properties are synonyms for, and interchangable with, the x and y event object properties.

layerY

Syntax: event.layerY

These properties returns a number that represents the horizontal/vertical position, in pixels, of the cursor relative to the layer that initiated the event, or, when passed with a resize event, it represents the object width/height. These properties are synonyms for, and interchangable with, the x and y event object properties.

modifiers

Syntax: event.modifiers

This property returns a string containing details of any modifier keys that were held down during a key or mouse event. The values of the modifier keys are as follows: ALT_MASK, CONTROL_MASK, SHIFT_MASK and META_MASK.

pageX

Syntax: event.pageX

These properties return the horizontal/vertical position of the cursor relative to the page, in pixels, at the time the event occured.

pageY

Syntax: event.pageY

These properties return the horizontal/vertical position of the cursor relative to the page, in pixels, at the time the event occured.

prototype

Syntax: Object.prototype.name = value

This allows the addition of properties and methods to any object.

screenX

Syntax: event.screenX

These properties return the horizontal/vertical position of the cursor relative to the screen, in pixels, at the time the event occured.

srcElement

Syntax: event.srcElement[ = oObject ]

Sets of retrieves te object that fired the event.

target

Syntax: event.target

This property returns a reference to the object that the event was originally sent to.

type

Syntax: event.type

This property returns a string that represents the type of the event (click, key down, etc.).

which

Syntax: event.which

This property returns a number that represents either which mouse button (1 being the left button, 2 the middle and 3 the right) was pressed or which key was pressed (its ASCII value) at the time the event occuered.

width

Syntax: event.width

This property relates to the height of the window or frame that contains the object that initiated the event.

x

Syntax: event.x

These properties returns a number that represents the horizontal/vertical position, in pixels, of the cursor relative to the layer that initiated the event, or, when passed with a resize event, it represents the object width/height. These properties are synonyms for, and interchangable with, the layerX and layerY event object properties.

y

Syntax: event.y

These properties returns a number that represents the horizontal/vertical position, in pixels, of the cursor relative to the layer that initiated the event, or, when passed with a resize event, it represents the object width/height. These properties are synonyms for, and interchangable with, the layerX and layerY event object properties.

Methods

eval

Syntax: Object.eval(string)

The eval method is deprecated as a method of Object, but is still used as a high level function. It evaluates a string of JavaScript in the context of an object.

toSource

Syntax: Object.toSource()

The toSource method returns a literal representing the source code of an object. This can then be used to create a new object.

toString

Syntax: Object.toString()

The toString method returns a string representing a specified object.

unwatch

Syntax: Object.unwatch(property)

This method removes a watchpoint set for an object and property name with the watch method.

valueOf

Syntax: Object.valueOf()

This method returns a primitive value for a specified object.

watch

Syntax: Object.watch(property, handlerfunction)

This method adds a watchpoint to a property of the object.

See Also: