Share via


Working with Code-Only Scriptlets

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.

There are two ways to create this kind of scriptlet. You can create an HTML file with <SCRIPT> tags to contain your custom procedures, or you can use the Scriptlet Wizard. In either case, you are creating a basic template with procedures that represent the public properties and methods of the object along with any private procedures available only to other procedures in the scriptlet object itself.

The following sample illustrates the HTML code and script for a very simple (no provision is made for handling errors) code-only scriptlet named GetRecords.htm.

You create properties for the scriptlet by adding the get_ (read) or put_ (write) prefix to the property name in the Function procedure. If you create both get_ and put_ procedures for a property, the property will be read/write. Using only one of the two designators creates a read-only or write-only property. Procedures created with the get_ and put_ designators are analogous to the Property Get and Property Let procedures used in a Microsoft® Visual Basic® for Applications (VBA) class module. You specify default values for properties by setting each property to some value at the top of the script block, before the procedures are defined.

You create public methods for the scriptlet by adding the public_ prefix to the name of the method in the Function procedure. Procedures that do not use the public_ prefix will be available only to other procedures within the scriptlet and will not be visible from the container Web page.

<HTML>
<HEAD>
<TITLE>GetRecords Scriptlet</TITLE>
</HEAD>

<SCRIPT LANGUAGE="VBScript">
<!--
' Initialize properties to their default values.
Dim p_provider
Dim p_databasePath

p_provider = "Microsoft.Jet.OLEDB.4.0"
p_databasePath = "c:\program files\microsoft office\office10\samples\northwind.mdb".  

Function get_provider()
   get_provider = p_provider
End Function

Function put_provider(newValue)
   p_provider = newValue
End Function

Function get_databasePath()
   get_databasePath = p_databasePath
End Function
Function put_databasePath(newValue)
   p_databasePath = newValue
End Function

Function public_GetRecordset(strSQL)
   Dim rstRecordset
   Dim cnnConnection

   Set cnnConnection = CreateObject("ADODB.Connection")
   cnn.Provider = p_provider
   cnn.Open p_databasePath

   Set rstRecords = CreateObject("ADODB.Recordset")
   rstRecords.Open strSQL, cnn, 1, 3
   Set public_GetRecordset = rstRecords
End Function
-->
</SCRIPT>

<BODY>
</BODY>
</HTML>

In the container Web page, you create a reference to this scriptlet by using an <OBJECT> tag as shown earlier for DHTML scriptlets:

<OBJECT
   ID="NWIND" 
   STYLE="position:absolute;
          width:0;
          height:0;
          top:0;
          left:0;"
   TYPE="text/x-scriptlet"
   DATA="GetRecords.htm">
</OBJECT>

The <OBJECT> tag is using an ID attribute setting of "NWIND", and you use this identifier when calling procedures in the scriptlet. For example, you would use the following script in the container Web page to call the custom GetRecordset method of the referenced scriptlet.

<SCRIPT LANGUAGE="VBScript">
<!--
Sub DisplayCustomerInfo()
   Dim strCountry
   Dim strSQL
   Dim rstRecords

   strCountry = document.all("txtCustomerCountry").value
   strSQL = "SELECT * FROM Customers WHERE Country = '" & strCountry & "'"
   Set rstRecords = NWIND.GetRecordset(strSQL)
   ' Work with the returned records here.
End Sub
-->
</SCRIPT>

This scriptlet is useful and flexible enough to work against any number of data sources. All you have to do is specify a different data source by setting the properties and then return matching records defined by your strSQL variable. As you can see from the preceding example, using scriptlets can be a very powerful technique for encapsulating related script procedures into reusable objects.

See Also

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