Share via


Exposing Properties and Methods in Behavior Script Components

Behavior script components can expose custom properties and methods in a manner similar to Automation script components. Behavior script component properties and methods extend the properties and methods available to elements in the containing page. For example, you might create a behavior script component that changes the color of an element when the mouse is passed over it. By defining a property in the script component, you can make available a custom property in the document, perhaps called hiliteColor, that allows the Web page author to set the color with which text gets highlighted.

A behavior can override an element's default behavior by exposing a property or method of the same name as that which is already defined for the element.

Properties and methods are defined in a <public> element separate from the <implements> element used to specify the Behavior handler. For details, see Exposing Properties and Exposing Methods.

Exposing a Property

Properties are exposed in a <public> element, as in any script component. The following script component fragment shows a Behavior script component that exposes the custom property hiliteColor. If the containing page does not specifically set the property's value, the default is set to "red."

Note

A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.

<public>
   <property name="hiliteColor"/>
</public>

<implements type="Behavior">
   <attach for="window" event="onload" handler="event_onload">
</implements>

<script language="JScript">
<![CDATA[
   var hiliteColor;
   function event_onload(){
      // Initialize the properties.
      if (hiliteColor == null){
         hiliteColor = "red";}
   }
   // Further script here.
]]>
</script>

Exposing a Method

Exposing a method in a Behavior script component is identical to doing so in an Automation script component. For details, see Exposing Methods. In a Behavior script component, methods exposed in the script component extend those already available for the element in the containing document.

See Also

Concepts

Exposing Custom Events in Behavior Script Components