Attaches a behavior to the element.
- bstrUrl [in]
A String 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.
- pvarFactory [in, optional]
Type: VARIANTThe address of a Variant structure to an IElementBehaviorFactory interface for the behavior.
Type: IntegerReturns an identifier that can be used later to detach the behavior from the element.
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).
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 behavior-defined member 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.
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 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:
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 addBehavior call is one of the default behaviors built into Internet Explorer, the 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.
This example uses the addBehavior method to dynamically attach a behavior that implements a mouseover highlighting effect to all li elements on a page.
- input type=button
- input type=checkbox
- input type=email
- input type=file
- input type=hidden
- input type=image
- input type=number
- input type=password
- input type=radio
- input type=range
- input type=reset
- input type=search
- input type=submit
- input type=tel
- input type=text
- input type=url
- Introduction to DHTML Behaviors
- Using DHTML Behaviors
- About Element Behaviors
- Using HTML Components to Implement DHTML Behaviors in Script