addBehavior method

Attaches a behavior to the element.


 retVal = object.addBehavior(bstrUrl, pvarFactory);


bstrUrl [in]

Type: BSTR

A BSTR that specifies one of the following values.


Location of the behavior, in URL format. The URL must be bounded by quotation marks, as illustrated in the following sample.


One of the Internet Explorer default behaviors, identified by its behavior name.


The IHTMLElement::id attribute that is specified in an object tag for the binary implementation of a behavior.

pvarFactory [in, optional]


The address of a VARIANT structure to an IElementBehaviorFactory interface for the behavior.

Standards information

There are no standards that apply here.


Starting with Internet Explorer 10, this method is obsolete and should not be used. For more info, see Element behaviors and HTCs are no longer supported.

This method enables you to attach a behavior without using Cascading Style Sheets (CSS).

Note A behavior attached to an element using the IHTMLElement2::addBehavior method, or by applying the proposed CSS IHTMLRuleStyle2::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.

Behaviors are subject to cross-frame security rules where a document cannot refer to a behavior on another server or another domain. In this case, the IHTMLElement2::addBehavior method returns E_ACCESSDENIED. For more information, see the Security section in the Introduction to DHTML Behaviors article.

The bstrUrl argument is always treated as the URL of the behavior and not the string name. For example, if bstrUrl is specified as SIMPLE, no behavior is initialized because MSHTML attempts to find the SIMPLE IElementBehaviorFactory interface in the local file system. If #factory#behaviorName is passed as the bstrUrl argument, then MSHTML parses the string into two parts: factory and behaviorName. MSHTML then calls factory and attempts to instantiate behaviorName, by passing behaviorName as the bstrBehavior argument of the FindBehavior method.

The pvarFactory argument is optional, and has no effect on the interpretation of the bstrUrl argument; therefore, passing a pointer to the pvarFactory argument is not a substitute for specifying the behavior factory in the bstrUrl argument.

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

This method, and the following remarks, apply only to attached behaviors, which are the original Dynamic HTML (DHTML) behaviors introduced in Microsoft Internet Explorer 5. Element behaviors are a feature in Microsoft Internet Explorer 5.5 and cannot be added to or removed from an element.

See also




Introduction to DHTML Behaviors

Using DHTML Behaviors

About Element Behaviors

Using HTML Components to Implement DHTML Behaviors in Script