Click to return to the DHTML, HTML     
add Method     AddChannel Method     DHTML Methods    
Web Workshop  |  DHTML, HTML & CSS

addBehavior Method


Attaches a behavior to the element.

Syntax

iID = object.addBehavior(sUrl)

Parameters

sUrl Required. String that specifies one of the following values:
sValue Location of the behavior, in URL format.
#default#behaviorName One of Internet Explorer's default behaviors, identified by its behaviorName.
#objID Binary implementation of a behavior, where objID is the ID attribute specified in an OBJECT tag.

Return Value

Integer. Returns an identifier that can be used later to detach the behavior from the element.

Remarks

This method lets you attach a behavior without having to use cascading style sheets (CSS).

Unless the behavior specified in the addBehavior call is one of the default behaviors built into Microsoft® Internet Explorer, the addBehavior call causes Internet Explorer to download the behavior asynchronously, before the behavior is attached to the element.

Due to the asynchronous nature of the addBehavior method, its return value cannot be relied on to determine whether the behavior was successfully applied to the element. Waiting for the onreadystatechange event to fire and verifying that the readyState property of the element is set to complete ensures that the behavior is completely attached to the element, and that all the behavior's members are available for scripting. Otherwise, attempting to use any of the behavior-defined members before the behavior is attached to the element results in a scripting error indicating that the object does not support that particular member.

Note A behavior attached to an element using the addBehavior method, or by applying the proposed CSS behavior attribute inline, is not automatically detached from the element when the element is removed from the document hierarchy. However, a behavior attached using a style rule defined in the document is detached automatically as the element is removed from the document tree.

Example

This example uses the addBehavior method to dynamically attach a behavior that implements a mouseover highlighting effect to all LI elements on a page.

Sample Code

<SCRIPT LANGUAGE="JScript">
var collBehaviorID = new Array();
var collLI = new Array ();
var countLI = 0;

function attachBehavior()
{
   collLI = document.all.tags ("LI");
   countLI = collLI.length;
    for (i=0; i < countLI; i++)
{
   var iID = collLI[i].addBehavior ("hilite.htc");
   
       if (iID)   
          collBehaviorID[i] = iID;
    } 
}

</SCRIPT>

:
//Click <A HREF="javascript:attachBehavior()">here</A> 
//to add a highlighting effect as you hover over each item below.
:
This feature requires Microsoft® Internet Explorer 5 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
A, ACRONYM, ADDRESS, APPLET, AREA, B, BASE, BASEFONT, BGSOUND, BIG, BLOCKQUOTE, BODY, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, custom, DD, DEL, DFN, DIR, DIV, DL, 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, ISINDEX, KBD, LABEL, LEGEND, LI, LINK, LISTING, MAP, MARQUEE, MENU, NEXTID, NOBR, NOFRAMES, NOSCRIPT, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, Q, RT, RUBY, S, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, STRONG, STYLE, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TITLE, TR, TT, U, UL, VAR, WBR, XML, XMP

See Also

removeBehavior, DHTML Behaviors, Using DHTML Behaviors, Implementing DHTML Behaviors in Script



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.