20 out of 44 rated this helpful - Rate this topic

attachEvent method

[This documentation is preliminary and is subject to change.]

Binds the specified function to an event, so that the function gets called whenever the event fires on the object.

Syntax

object.attachEvent(event, pDisp)

Standards information

There are no standards that apply here.

Parameters

event [in]

Type: BSTR

String that specifies any of the standard DHTML Events.

pDisp [in]

Type: IDispatch

Pointer that specifies the function to call when event fires.

Return value

Type: BooleanBoolean. Returns one of the following possible values:
Return valueDescription
true

The function was bound successfully to the event.

false

The function was not bound to the event.

 

Remarks

When event fires on the object, the object's event handler is called before pDisp, 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.

Code example: http://samples.msdn.microsoft.com/workshop/samples/components/htc/refs/ondetach.htm


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

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>


See also

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
Reference
detachEvent
Conceptual
Introduction to DHTML Behaviors
Using DHTML Behaviors
Using HTML Components to Implement DHTML Behaviors in Script

 

 

Build date: 3/14/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Alternatives to AttachEvent
Due to the fact that IE does not follow standards, this is work around:

/*** 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;
}
}
How to Translate attachEvent to addEventListener

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) });
    }
}
Watch the GetRef in VBS

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