Internet Explorer

Internet Explorer supports dynamic HTML content by enabling developers to combine HTML with scripting code on the client system. The scripts are typically written in either VBScript or JScript and executed on the local system. Unlike ASP, where the Scripting Edition components reside on the server, the components must be redistributed to the client system to use them with Internet Explorer.

Creating Objects

There are two basic ways in which Scripting Edition components can be used in Internet Explorer. The first method is the familiar approach of using the CreateObject function to create an instance of the component. This is the simplest approach, and is best suited for those scripts which do not use events. The following is an example of some code used to create an instance of the File Transfer Protocol object:

<script language="vbscript">
<!--
Option Explicit

Dim ftpClient
Set ftpClient = CreateObject("SocketTools.FtpObject.6")
-->
</script>

The argument to the CreateObject function is called the programmatic ID (ProgID) for the component. It is a string which identifies the object that is to be created, and each scripting component has a unique name. To determine what value should be passed for a specific component, refer to the Technical Reference documentation. Once an instance of the object has been created, you can reference it just as you would any other ActiveX control or COM object.

A second method for creating an instance of the control in the web page is to use the <object> tag. This approach is best suited for those scripts which need to use events, such as the OnProgress event in the FTP component. The following is an example of how the object can be created:

<object classid="clsid:E5B9E10D-71EB-4ED9-A585-B7D9B1C320C2" id="ftpClient"></object>

The classid parameter specifies the unique 128-bit number that identifies the COM object. Note that the "clsid:" prefix must be included along with the class ID itself. The id parameter specifies the name that will be used to reference the object in the scripting code. Note that each of the Scripting Edition components have their own unique class ID. To determine what value should be specified for a specific component, refer to the Technical Reference documentation.

Initialization

After an instance of the object has been created, the next step that you need to take is to call the Initialize method. This will prepare the component for use, validating the runtime license, loading any required networking libraries and allocating the system resources that it requires. The Initialize method has several optional arguments, however in most cases the only argument that is required is the runtime license key. This is a string of characters which is used to validate your development license. It is important to note that the runtime key is not your product serial number. For more information, refer to the section on Component Initialization. The following code demonstrates how the FTP component can be initialized:

<script language="vbscript">
<!--
Dim strLicenseKey
Dim nError

strLicenseKey = "" ' Set this to the runtime license key string
nError = ftpClient.Initialize(strLicenseKey)
If nError <> 0 Then
    MsgBox "Unable to initialize SocketTools component", vbExclamation
End If
-->
</script>

Good coding practice dictates that for every time the Initialize method is called, it should be matched with a call to the Uninitialize method before the object is destroyed or the script terminates. This tells the component to release any resources that were allocated. Note that if your script does not explicitly call the Uninitialize method, it will be done automatically when the object is destroyed.

Event Handling

Event notification is the means by which the component informs the application of a change in the status of the current session. For example, when the GetFile method is used in the FTP component, as the file is being downloaded the OnProgress event will be periodically fired so that the script can update any user interface objects, such as a ProgressBar control. There are two general methods for implementing event handling for a component in Internet Explorer. The first is called "automagic" event handling, where the name of the event handler is based on the name of the component and the event. For example, the following code demonstrates updating a ProgressBar control during a file transfer:

<script language="vbscript">
<!--
Sub ftpClient_OnProgress(ByVal FileName, ByVal FileSize, ByVal BytesCopied, ByVal Percent)
    Form1.ProgressBar1.Value = Percent
End Sub
-->
</script>

One of the important things to note is that the arguments to the event handler are passed by value, not by reference. Therefore, in VBScript the ByVal keyword needs to be used. As with method arguments, the event arguments are variants. An alternative method of creating an event handler is to use the <script> tag to define a handler for that object:

<script for="ftpClient" language="vbscript"
 event="(ByVal FileName, ByVal FileSize, ByVal BytesCopied, ByVal Percent)">
<!--
    Form1.ProgressBar1.Value = Percent
-->
</script>

In both cases, the scripting component must be created using the <object> tag in order to use events. It is important to keep in mind that the event handler signature (the number of arguments passed to the event handler) must match exactly, otherwise the event handler code will not be called.


Copyright © 2008 Catalyst Development Corporation. All rights reserved.