Understanding Behaviors

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Behaviors provide a way to create reusable components you can use on a Web page. They make it possible for you to encapsulate and reuse blocks of code and to separate the script in a page from its content. There are two types of behaviors you can use with your Web pages: custom behaviors and built-in behaviors.

Custom Behaviors

Custom behaviors are integrated with Microsoft® Internet Explorer through an interface handler. Internet Explorer provides the Behavior Handler (a type of interface handler) as the way a behavior scriptlet can communicate with the page that references it. The Behavior Handler makes it possible for the behavior scriptlet to expose custom events, access the containing page's DHTML object model, and receive notification when an event has occurred in the containing page.

The basic parts of a custom behavior scriptlet are <SCRIPTLET> tags, <IMPLEMENTS> tags, and <SCRIPT> tags, which are saved in a file that has an .sct extension. The <IMPLEMENTS> tag uses a TYPE attribute of "Automation". The <SCRIPT> tags contain the script you want to associate with the behavior.

<SCRIPTLET>
<!--
   <IMPLEMENTS Type="Behavior">
   </IMPLEMENTS>
   <SCRIPT Language="VBScript">
      ' Add your script here.
-->
   </SCRIPT>
</SCRIPTLET>

When you have created this basic framework, you can use DHTML objects and methods to create custom events, synchronize with events generated by an element in the containing page, or expose custom properties and methods. The following example serves to illustrate the concepts behind creating a custom behavior scriptlet. This behavior uses the attachEvent method to monitor events from the containing page and execute script when the specified events occur:

<SCRIPTLET>
   <IMPLEMENTS Type="Behavior">
   </IMPLEMENTS>
   <SCRIPT Language="VBScript">
<!--
      Dim previousColor

      attachEvent("onmouseOver", event_onmouseOver)
      attachEvent("onmouseOut", event_onmouseOut)

      Function event_onMouseOver()
         previousColor = style.color
         style.color = "red"
      End Function

      Function event_onMouseOut()
         style.color = previousColor
      End Function
-->
   </SCRIPT>
</SCRIPTLET>

When this behavior is connected to an HTML element as a STYLE attribute, the script it contains runs in response to the mouseOver and mouseOut events. When the mouse pointer moves over the element, the element color is changed to red and the element's original color is saved in the previousColor variable. When the mouse pointer moves off the element, the original color is restored. You can add this behavior to an HTML element by specifying the behavior scriptlet as a STYLE attribute of the element, as shown in the following example:

<HTML>
<TITLE>Behavior Demo</TITLE>
<HEAD>
<STYLE>
   .HiLite {behavior:url(hilite.sct)}
</STYLE>
</HEAD>
<BODY>
This word is <SPAN style=hilite>CAPITALIZED</SPAN>. Move the mouse pointer over it to change its color.
</BODY>
</HTML>

Built-in Behaviors

In addition, you can take advantage of the built-in, or default, behaviors included with Internet Explorer. These behaviors are available directly from the browser; you can use them without writing any additional external components yourself. The behaviors implemented by Internet Explorer are listed in the following table.

Built-in behavior Description
anchorClick Enables browser navigation to a folder view.
clientCaps Provides information about the capabilities Internet Explorer supports, as well as a means to install browser components on demand.
download Provides a means to download a file and notify a callback function when the download is complete.
homePage Contains information about a user's home page.
httpFolder Contains script features that enable browser navigation to a folder view.
saveFavorite Makes it possible for the current state of a page to be saved when it is added to Favorites.
saveHistory Makes it possible for the current state of the page to be saved when the user navigates away from the page.
saveSnapshot Makes it possible for persistence of information (form values, styles, dynamically updated content, and script variable values) when a page is saved locally.
userData Makes it possible for persistence of information across sessions by writing data to Extensible Markup Language (XML) storage.
Anim Defines an instance of the Microsoft DirectAnimation viewer in an HTML document to render DirectAnimation objects and play DirectAnimation sounds.

You reference a built-in behavior by using the behavior's name and #default#BehaviorName as the value for the URL argument, as shown in the following example:

<HTML>
<TITLE>Built-in Behavior Demo</TITLE>
<HEAD>
<STYLE>
   .saveFavorite {behavior:url(#default#savefavorite)}
</STYLE>
</HEAD>
<BODY>
<INPUT class=saveFavorite type=text width=35 value="I am a text box.">
</BODY>
</HTML>

See Also

Working with Office Web Discussions Client | Enabling Discussions | Understanding the Global Object | Understanding Discussion Servers | Understanding Discussions | Understanding Subscriptions