addBehavior method

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

Attaches a behavior to the element.

Syntax


 retVal = object.addBehavior(bstrUrl, pvarFactory);

Standards information

There are no standards that apply here.

Parameters

bstrUrl [in]

Type: BSTR

A BSTR that specifies one of the following values.

sValue

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

#default#behaviorName

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

#objID

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

pvarFactory [in, optional]

Type: VARIANT

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

Remarks

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

Reference
removeBehavior
Conceptual
Introduction to DHTML Behaviors
Using DHTML Behaviors
About Element Behaviors
Using HTML Components to Implement DHTML Behaviors in Script

 

 

Build date: 6/6/2012

Community Additions

Show:
© 2014 Microsoft