Windows Script Components  

Creating a Behavior Script Component

Creating a Behavior script component is similar to creating any other kind of script component, except that you are linking Microsoft® Internet Explorer events to script that is run in response to those events.

This topic is divided into the following sections:

Creating a Behavior Script Component File

A Behavior script component includes an <implements> element that specifies the Behavior interface handler. Within the <implements> element, you use:

Behavior script components can also include custom properties and methods that extend those already available for the element in the containing document. For details, see Exposing Properties and Methods in Behavior Script Components.

The following example shows a Behavior script component that changes the color of an element in the containing page whenever the mouse is passed over the element. To do this, the sample binds the DHTML onmouseover and onmouseout events to functions in the script component that set the element's DHTML style attribute. The sample also sets the document's link color when the document is initialized by binding to the DHTML window object's onload event.

In addition to binding events to script, the script component also inserts text into any <H1> elements in the containing document that are linked to this script component. Finally, it exposes and fires an event called onchange, which extends the DHTML window object's event object with a custom property called newvalue.

Note   A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.
<?XML version="1.0"?>
<component>
<implements type="Behavior">
   <comment>The following will cause the do_nmousedown and 
   do_mouseout functions to be called when the mouse is 
   passed over the element in the containing document.</comment>

   <attach event="onmouseover" handler="do_onmouseover"/>
   <attach event="onmouseout" handler="do_onmouseout"/>

   <comment> This will call the init function when the onload 
   event of window is fired.</comment>

   <attach for="window" event="onload" handler="docinit"/>

   <comment>The following defines HTML text that will appear in 
   the containing document.</comment>

   <layout>
      <h1>This is the HTML to show in the element</h1>
   </layout>

   <comment>The following defines a custom event that is fired 
   from within the script component by the fireEvent method.</comment>

   <public>
      <event name="onchange"/>
   </public>

</implements>
<script language="JScript">
<![CDATA[
var normalColor, normalSpacing;
function do_onmouseover(){
   // Save original values.
   normalColor = style.color; 
   normalSpacing= style.letterSpacing;
   style.color = "red";
   style.letterSpacing = 2;
   oEvent = createEventObject();
   oEvent.newcolor = "red";
   fireEvent("onchange",oEvent);
}
function do_onmouseout(){
   // Reset to original values.
   style.color = normalColor;
   style.letterSpacing = normalSpacing;
}

function docinit(){
   document.linkColor = "red";
}
]]>
</script>
</component>

There are a few things to point out in the preceding code:

Behavior-Related Enhancements to the DHTML Object Model

The following enhancements were made to the DHTML object model for Microsoft® Internet Explorer 5 in order to add support for behaviors.

Getting Event Parameters in the Script Component

In DHTML, the DHTML event object provides information about the event. Although in DHTML the event object is accessible to event handlers through the DHTML window object, in a behavior script component the event object is passed in as a parameter to the event handler.

The following code from a hypothetical calculator script component demonstrates keyboard and mouse events bound to a script component function called doCalc. The doCalc function uses the event object to get information about the conditions under which the event was fired.

Note   A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.
<implements type="Behavior">
   <attach event="onclick" handler="doCalc"/>
   <attach event="onkeydown" handler="doCalc"/>
</implements>

<script language="jscript">
<![CDATA[
function doCalc(oEventParam){
   oElement = oEventParam.srcElement;
   if(oEventParam.type == "keydown"){
      sVal = KeyCodeToChar(oEventParam.keyCode);
   }
   else{
      if (oEventParam.srcElement.type != "button"){
         return;}
      sVal = stripBlanks(oEventParam.srcElement.value);
   }
}
// other script here
]]>
</script>

Scope Rules

When working with script components, you actually work with three namespaces: the behavior, the element, and the containing document. Scope rules define the order in which name conflicts are resolved in a behavior script component. A name will be resolved in this order:

The name is resolved to one defined by the behavior anywhere in the script component, whether a variable, a behavior-defined property, a method, or an event.

If the name is not resolved successfully, it is resolved to a property, method, or event that applies to the element.

Finally, the name is resolved to the name of a property, method, or event that applies to the window object in the containing page.

In the following example, note how the names have been resolved, using the scope rules defined above:

Timing Considerations

When creating behaviors, it is important to know when the behavior is applied to the element. Until the behavior has been applied, scripts cannot have access to the values of behavior-defined properties that might be set in the document.

Because the behavior is encapsulated in a separate file from the HTML document, it is downloaded separately from the rest of the document. As the document and behavior are parsed and loaded, the behavior receives notifications of progress through the function specified with the attachNotification method. Currently, the behavior is notified with a "contentChange" or a "documentReady" notification. The "contentChange" notification is sent when the content of the element to which the behavior has been attached has been parsed, and any time thereafter that the content of the element is changed. The "documentReady" notification is sent when the document has been downloaded and parsed.

Because inline script in the script component file is executed as soon as the behavior is instantiated, the values of behavior-defined attributes and properties that are being set in the document may not be accessible from an inline script. However, these properties will be available as soon as the first "contentChange" notification is sent.

See Also

Exposing Properties and Methods in Behavior Script Components | Exposing Custom Events in Behavior Script Components | Behavior Handler Reference