Partager via


addBehavior method

Attaches a behavior to the element.

Syntax

*object.*addBehavior(bstrUrl, pvarFactory)

Parameters

bstrUrl [in]

Type: String

A String 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 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.

Return value

Type: Integer

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

Standards information

There are no standards that apply here.

Remarks

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

Examples

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

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

<script type="text/javascript">
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.
:

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

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

ins

isIndex

kbd

label

legend

li

link

listing

map

mark

marquee

menu

nav

nextID

noBR

noFrames

noScript

object

ol

option

p

plainText

pre

q

rt

ruby

s

samp

script

section

select

small

span

strike

strong

style

sub

sup

table

tBody

td

textArea

tFoot

th

tHead

title

tr

tt

u

ul

var

wbr

xml

xmp

Reference

removeBehavior

Conceptual

Introduction to DHTML Behaviors

Using DHTML Behaviors

About Element Behaviors

Using HTML Components to Implement DHTML Behaviors in Script