Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
Binds the specified function to an event so that the function gets called whenever the event fires on the object.
Syntax
bSuccess = object.attachEvent(sEvent, fpNotify)
Parameters
sEvent Required. Specifies the name of the event. fpNotify Required. Specifies the function to be called whenever sEvent fires.
Return Value
Boolean. Returns TRUE if the function was bound successfully to the event, or FALSE otherwise.
Remarks
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.
Example
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>
Applies To
A, ACRONYM, ADDRESS, APPLET, AREA, B, BASE, BASEFONT, BGSOUND, BIG, BLOCKQUOTE, BODY, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, DD, DEL, DFN, DIR, DIV, DL, document, DT, EM, EMBED, FIELDSET, FONT, FORM, FRAME, FRAMESET, HEAD, Hn, HR, HTML, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, KBD, LABEL, LEGEND, LI, LINK, LISTING, MAP, MARQUEE, MENU, NOBR, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, Q, S, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TITLE, TR, TT, U, UL, VAR, window, XMP
See Also
detachEvent, Scriptlets Overview
Does this content meet your programming needs? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.