DHTML Methods
Binds the specified function to an event so that the function gets called whenever the event fires on the object.
bSuccess = object.attachEvent ( sEvent, fpNotify )
sEvent |
Required. Specifies the name of the event. |
fpNotify |
Required. Specifies the function to be called whenever sEvent fires. |
Boolean. Returns TRUE if the function was bound successfully to the event, or FALSE otherwise.
When sEvent fires on the object, the object’s sEvent handler is called first, before the specified function, fpNotify. If multiple functions are attached to the same event on the same object, the functions will be called in a random order, immediately after the object’s event handler is called.
This method provides a means for a behavior to listen in on events that occur on the containing page. It is not limited, however, to behaviors. A function defined on a page can be attached to events fired on the same page as well.
The following example implements a mouseover highlight effect using a behavior. The sample demonstrates two ways of attaching to events: using the ATTACH element defined for HTML Components ( HTC ), and using the attachEvent method. The ATTACH element is simply a declarative form of the attachEvent method.
Sample Code
<property name="hiliteColor" />
<attach event=onmouseover
handler=event_onmouseover />
<attach event=onmouseout
handler=event_onmouseout />
<script language="JScript">
var normalColor;
window.attachEvent ( "onload", event_onload );
function event_onload ( ) {
// initialize the properties
if ( hiliteColor == null )
hiliteColor = "red";
}
function event_onmouseover ( ) {
normalColor = style.color;
style.color = hiliteColor;
}
function event_onmouseout ( ) {
style.color = normalColor;
}
</script>
detachEvent