Windows Script Components  

Exposing Custom Events in Behavior Script Components

Behavior script components, like Automation script components, can expose custom events that are fired inside the script component and can be handled in the containing document. An event is declared in the <public> element as in the following:

   <event name="onResultChange" />

The event can then be fired by calling the fireEvent method in script:

<script language="JScript">
   // Other code here.
   // Other code here.

A behavior can override an element's default behavior by exposing an event of the same name as one that is already defined for the element. For example, a behavior that exposes an onclick event can override the element's default onclick event.

Getting and Setting Custom Event Information

Your custom script component event can get access to the DHTML event object, which maintains event-specific information. You can use this object to:

In your script component code, call the createEventObject method to create a new instance of an event object, and then set one or more properties on the new event object. When you call the fireEvent method, you can pass the new event object with the event name.

To create a new expando property, simply name it when assigning its value in your script. The following shows how you can create a new property called myprop for the event object:

oEvent = createEventObject();
oEvent.myprop = "a new value"
Note   You can create expando properties only if you are using Microsoft® JScript® (or JavaScript). This feature is not supported in Microsoft® Visual Basic® Scripting Edition (VBScript).


The following script component fragment is derived from a calculator script component sample. The sample defines a custom onResultChange event that is fired to the containing document whenever the result changes. Event-specific information (the actual calculation result) is passed via the expando property called result.

Note   A CDATA section is required to make the script in the <script> element opaque. For details, see Script Component Files and XML Conformance.
   <event name="onResultChange" />

<implements type="Behavior">
   <attach event="onclick" handler="doCalc"/>

<script language="JScript">
function doCalc()
   // Code here to perform calculation. Results are placed into
   // the variable sResult.
   oEvent = createEventObject();
   oEvent.result = sResult;

The following shows what the containing page looks like. When the onResultChange event fires, the results of the calculation are extracted from expando property result of the DHTML window.event object and displayed in the resultWindow <DIV> element.

<xml:namespace prefix="LK" />
   LK\:CALC {behavior:url(calc.wsc)}
<script language="JScript">
function showResults(){

<LK:CALC id="myCalc" onResultChange="showResults()">
   <DIV ID=resultWindow 
      STYLE="border: '.025cm solid gray'" 

See Also

Exposing Properties and Methods in Behavior Script Components