Defining Properties and Methods in DHTML Scriptlets

DHTML scriptlets can expose any number of properties and methods. You can do so in two ways:

In general, using a public_description object is associated with using JavaScript, and using default interface descriptions with using VBScript. However, the only strict language requirement is that if you use a public_description object, the public_description object itself must be created in JavaScript. Any additional functions, such as those to define properties and methods, can be written in any scripting language. Similarly, although using default interface descriptions is associated with VBScript, you can actually write the functions in any language.

Using a public_description object has several advantages. You can use any names for variables and functions that you want to expose as properties and methods, because you assign them public names in the public_description object. In addition, using the public_description object provides you with a convenient way to summarize and document the properties and methods that the scriptlet exposes.

Creating a public_description Object

A public_description object is a JavaScript object that provides run-time access to the properties and methods defined by the object's constructor function. Any behavior that is not explicitly declared in the constructor is not available.

Note   You must use the name public_description in lowercase characters as shown, or the scriptlet's properties and methods will not be exposed properly.

A skeleton public_description with its constructor function looks like this:

<SCRIPT LANGUAGE="JavaScript">
   var public_description = new CreateScriptlet();
   Function CreateScriptlet(){
      // statements here to define properties and methods
   }
</SCRIPT>

Note   You do not use the constructor function to define events. For details, see Exposing Events in Scriptlets.

When you create the public_description object, the constructor function that you assign to it can have any name, as long as the corresponding function appears somewhere in the scriptlet. In the constructor, declare the properties and methods you want to expose using the syntax listed in the following table.

Declaration in constructor Creates
This.PropertyName = expression; Property as an expression. When the user gets the property, the value of expression is passed to the calling container. When the user sets the property, the value of expression is updated.
This.get_PropertyName = function;

This.put_PropertyName = function;

Property as a function. The function called by the property definition can be in any active scripting language. To make a property read-only, do not provide the put_ function declaration; to make it write-only, do not provide the get_ function declaration.
This.method = methodFunction; Method.

For example, the following scriptlet includes a marquee that exposes properties and methods for setting its text and color. The constructor defines two properties: version, a simple value, and marqueetext, created as a set of functions that illustrate how to get and set property values conditionally.

The constructor also defines the method togglecolor, which calls a function in the scriptlet called setcolor:

<HTML>
<HEAD>
<TITLE>Scriptlets!</TITLE>
<SCRIPT LANGUAGE="JavaScript">
// public_description object used to declare scriplet
var public_description = new scriptletobject();

// general object description
function scriptletobject()
{
   this.version = "1.0";              //property
   this.get_marqueetext = readtext;   //property (read)
   this.put_marqueetext = writetext;  //property (write)
   this.togglecolor = setcolor;       //method
}

function readtext(){
   if (mrq1.innerText == ""){
      return "No text";}
   else{
      return mrq1.innerText;}
}

function writetext(newtext){
   if (newtext != ""){ 
      mrq1.innerText = newtext;}
}

function setcolor(){
   if (f1.color == "#ff0000"){
      // red, make it blue
      f1.color = "#0000ff";}
   else{
      // not red, make it red
      f1.color = "#ff0000";}
}
</SCRIPT>
</HEAD>

<BODY>
<B>Sample scriptlet</B><br>

<FONT ID="f1" COLOR="red">
<MARQUEE ID="mrq1">Scriptlets are fun and easy!</MARQUEE>
</FONT>
</BODY>
</HTML>

Creating Default Interface Descriptions

If there is no public_description object defined in the scriptlet, the scriptlet container object exposes properties and methods using variables and functions in the scriptlet that follow certain naming conventions.

To expose scriptlet properties and methods, use these conventions:

Note   When a property is exposed, its name in the host application does not have the public_ prefix. For example, if you define a property called public_MyTitle in the scriptlet, its name in the host application is MyTitle.

The following table shows examples of variables and functions in a scriptlet, and the resulting interface that they expose in the host application.

Example Exposed as Used in container
var public_Color = "red" Property vColor = SC1.Color

SC1.Color = "blue"

function public_get_C() Property (read) x = SC1.C
function public_put_C(param) Property (write) SC1.C = "test"
function public_look(param) Method SC1.look(param)
function look() Not available
(no public_ prefix)
function get_C() Not available
(no public_ prefix)
var Color = red; Not available
(no public_ prefix)
var get_Color = red; Not available
(no public_ prefix)

The following example shows a simple scriptlet containing a paragraph named "p1." The script block following the paragraph exposes a property called "text" and another called "color," which is defined using get and set functions that show how to set a property conditionally. The scriptlet also exposes a function called "settext."

<HTML><HEAD></HEAD>
<BODY>
<FONT ID="f1" color="black">
<P ID="p1">This is a paragraph of text.</P>
</FONT>

<SCRIPT LANGUAGE="JavaScript">
var public_text = p1.innerText;
function public_get_color(){
   return f1.color;
}
function public_put_color(color){
   f1.color = color;
}
function public_settext(newtext){
   p1.innerText = newtext;
}
</SCRIPT>
</BODY></HTML>

Note   The scriptlet reserves the function name prefixes public_get_ and public_put_ to define properties. For example, if the scriptlet contains a function named public_get_MyText, it will be treated as a property called MyText. If you attempt to call the function public_get_MyText as a method using the syntax SC1.get_MyText(), an error will result, because the function itself is exposed only as if it were a property named MyText.