attachEvent Method
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. String that specifies any of the standard DHTML Events. fpNotify Required. Pointer that specifies the function to call when sEvent fires.
Return Value
Boolean. Returns one of the following possible values:
true- The function was bound successfully to the event.
false- The function was not bound to the event.
Remarks
When sEvent fires on the object, the object's sEvent handler is called before fpNotify, the specified function. If you attach multiple functions to the same event on the same object, the functions are called in random order, immediately after the object's event handler is called.
The attachEvent method enables a behavior to handle events that occur on the containing page. This method is not limited, however, to behaviors. You can also define a function on a page that attaches to events fired on the same page.
Behaviors that attach to events using the attachEvent method must explicitly call the detachEvent method to stop receiving notifications from the page when the ondetach event fires.
A behavior that attaches to events on the page using the HTML Component (HTC) PUBLIC:ATTACH element automatically stops receiving notifications when the behavior detaches from the element, and does not need to call the detachEvent method.
Note To use the attachEvent method with Microsoft Visual Basic Scripting Edition (VBScript), you need to use the GetRef to obtain a function pointer. The function pointer can then be passed to attachEvent.
Examples
This example shows how to implement a mouseover highlighting effect by calling the attachEvent method from an HTC.
<PUBLIC:ATTACH EVENT="ondetach" ONEVENT="cleanup()" /> <SCRIPT LANGUAGE="JScript"> attachEvent ('onmouseover', Hilite); attachEvent ('onmouseout', Restore); function cleanup() { detachEvent ('onmouseover', Hilite); detachEvent ('onmouseout', Restore); } function Hilite() { if (event.srcElement == element) { normalColor = style.color; runtimeStyle.color = "red"; runtimeStyle.cursor = "hand"; } } function Restore() { if (event.srcElement == element) { runtimeStyle.color = normalColor; runtimeStyle.cursor = ""; } } </SCRIPT>Code example: http://samples.msdn.microsoft.com/workshop/samples/components/htc/refs/ondetach.htm
The following example shows how to use the attachEvent method with VBScript. When you click the span's text, the event handler changes the background color.
<HTML> <BODY ONLOAD="setupEventHandler()" LANGUAGE="VBS"> <SCRIPT LANGUAGE="VBS"> function setupEventHandler() set fpchgBackground = getRef("chgBackground") call mySpan.attachEvent("onclick", fpChgBackground) end function function chgBackground() document.bgColor = "lemonchiffon" end function </SCRIPT> <SPAN ID="mySpan">SPAN</SPAN> </BODY> </HTML>
Standards Information
There is no public standard that applies to this method.
Applies To
A, ABBR, ACRONYM, ADDRESS, APPLET, AREA, ARTICLE, ASIDE, B, BASE, BASEFONT, BGSOUND, BIG, BLOCKQUOTE, BODY, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, CUSTOM, DD, DEL, DFN, DIR, DIV, DL, document, DT, EM, EMBED, FIELDSET, FIGCAPTION, FIGURE, FONT, FOOTER, FORM, FRAME, FRAMESET, HEAD, HEADER, HGROUP, 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, MARK, MARQUEE, MENU, namespace, NAV, NOBR, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, Q, S, SAMP, SCRIPT, SECTION, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TITLE, TR, TT, U, UL, VAR, window, XMP, Element Constructor, HTMLDocument Constructor, HTMLNamespaceInfo Constructor, Window Constructor
See Also
/*** Adds a listener.
* @see https://developer.mozilla.org/en/DOM/element.addEventListener
* @see http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration
* @param eventName name of the event.
* @param handler callback function
* @param control control to attach, can be an Id or a control
*/
function addListener ( eventName, handler, control) {
//Check if control is a string
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators
if (control === String(control))
control = document.getElementById(control);
if(control.addEventListener) //Standard W3C
{
return control.addEventListener(eventName, handler, false);
} else if (control.attachEvent) //IExplore
{
return control.attachEvent("on"+eventName, handler);
}else{
return false;
}
}
- 4/29/2010
- zaiboot
- 7/29/2010
- John Sudds [Microsoft]
The W3C DOM Level 2 Event Model specifies addEventListener as the standard way to register event handlers. For cross-browser compatibility, the following script defines addEventListener to the window object.
if (!window.addEventListener) {
window.addEventListener = function (type, listener, useCapture) {
attachEvent('on' + type, function() { listener(event) });
}
}
- 9/29/2008
- John Sudds [Microsoft]
- 10/21/2009
- MalekaS
The link on this page is broken; try this one:
http://msdn.microsoft.com/en-us/library/ekabbe10(VS.85).aspx
Also note that:
GetRef() Does NOT work on class methods!!!
GetRef() Can't pass parameters
GetRef() Expects a text string which is the name of the function/sub, NOT the function name itself. This is counter-intuitive, but works...
Sub Foo
msgbox "Howdy"
End Sub
AttachEvent ( "onmouseover", GetRef ( Foo)) ' doh!
AttachEvent ( "onmouseover", GetRef ( "Foo")) ' works
- 1/26/2009
- OxG
