Chapter 18

Using ActiveX and Microsoft Internet Explorer 3.0


CONTENTS

In this chapter, you're taking a bit of a detour from JavaScript. You will be looking at Microsoft's answer to scripting. This includes the following topics:

Downloading and Installing Internet Explorer 3.0

Microsoft Internet Explorer (MSIE) is Netscape's greatest competition, and for good reason-it includes many enhancements and is completely free. However, it doesn't support JavaScript completely. Here are some highlights of the features in the latest version:

Tip
For more information about MSIE 3.0, ActiveX, and VBScript, take a look at Internet Explorer 3 Unleashed, published by Sams.net.

To begin, you need to download the latest version of MSIE. You can find the appropriate link to download it by starting at this URL:

http://www.microsoft.com/ie/

The Windows version of MSIE comes in a self-extracting archive file. To begin the installation, simply click on the file. Note that, in the current version, MSIE installs on drive C, whether you want it to or not.

Note
MSIE is available for Windows 95, Windows 3.1, and Macintosh platforms at this writing, although not all platforms have all features available. Consult Microsoft's Web page for further information.

Once installed, you will find an icon to run MSIE on the desktop. When you first run MSIE, the Microsoft Network page will be loaded by default, as shown in Figure 18.1. Luckily, you can change the default page.

Figure 18.1 : The initial display of MSIE includes Microsoft's Web page.

Internet Explorer and JavaScript

In this section, you'll take a look at the support for JavaScript in MSIE. Currently, some JavaScript is supported, but not all-perhaps half of the examples in this book will work with MSIE. Here are some of the key features that are missing:

These problems apply to the current version of MSIE (3.0 beta). Of course, future versions may implement JavaScript better. Because Netscape is still developing JavaScript, Microsoft can't get everything right until it's finished.

Note
Bear in mind that this book is based on Netscape 3.0's implementation of JavaScript, but MSIE's support more closely matches that of Netscape 2.0. A future version of MSIE will undoubtedly address the 3.0 features.

As one example, Figure 18.2 shows MSIE displaying the Fictional Software Company Web page with the scrolling message example from Chapter 8 "Improving a Web Page with JavaScript." The scrolling status-line message works fine, but the onMouseOver events to display friendly link descriptions don't.

Figure 18.2 : MSIE shows the FSC home page from Chapter 8.

Using ActiveX Controls

One of the latest buzzwords on the Internet today is ActiveX. ActiveX was developed by Microsoft and is a standard for controls-self-contained devices with their own functions-that can be placed on Web pages.

If you've heard of Object Linking and Embedding (OLE), you already know about ActiveX, because they're the same thing. OLE controls were called OCX controls for a while and are now called ActiveX controls. By the time you read this, they may already have a new name.

Regardless of the name, ActiveX controls can be powerful additions to a Web page. Better still, you can use them without necessarily knowing how they work. Currently, MSIE is the only browser to support ActiveX controls, but this will change-a plug-in for Netscape to support them is already in development, and Netscape has announced that it will support them in a future version.

Note
As you may have guessed, ActiveX is a Windows-only feature-at least for now. It is unknown whether Microsoft will support other platforms; ActiveX controls rely heavily on the Windows API, so this may never happen.

How ActiveX Controls Work

You can use an ActiveX control anywhere in a Web page using the <OBJECT> tag, which you learn about in the next section. ActiveX controls work similarly to Java-they are embedded in the page and perform a specific function. Compared to Java, though, they're more integrated with HTML.

Unlike Java, an ActiveX control can be permanently installed on your machine, making it available for future use. When you access a page with a control, it attempts to use an installed control. If you don't have the control installed, it downloads and installs it automatically. (It does inform you that it's doing this.)

Also unlike JavaScript and Java, ActiveX controls are purely executable code-they are handled by the operating system directly, and little work is done by the browser.

These controls can be quite large and complicated. They can be used to add functionality to the browser, similar to Netscape plug-ins. For example, the current version of MSIE implements the Java virtual machine as an ActiveX control.

You can create your own ActiveX controls-however, it's not as easy as JavaScript, or even Java. You need to use the Microsoft C compiler under Windows to create them.

Using an Existing ActiveX Control

Microsoft added the <OBJECT> tag to its implementation of HTML to support ActiveX controls. This tag works like Java's <APPLET> tag-it specifies the control to load and can optionally include parameters to be sent to the control.

As an example, Listing 18.1 shows an HTML file that embeds an ActiveX control. This control is the gradient control, which displays a range of colors in an area. Figure 18.3 shows the result of loading this page in MSIE.

Figure 18.3 : MSIE shows the HTML document with the embedded gradient control.


Listing 18.1. (GRADIENT.htm) An HTML document including an embedded ActiveX control.
<HTML>
<HEAD>
<TITLE>The ActiveX Gradient Control</TITLE>
</HEAD>
<BODY>
<H1>Gradient Control (ActiveX)</H1>
<HR>
<OBJECT
id=iegrad1
type="application/x-oleobject"
classid="clsid:017C99A0-8637-11CF-A3A9-00A0C9034920"
codebase="http://www.microsoft.com/ie/download/activex/
 iegrad.ocx#Version=4,70,0,1086"
width=500 height=50 >
<param name="StartColor" value="#ff0000">
<param name="EndColor" value="#0000ff">
<param name="Direction" value = "0">
</OBJECT>
<HR>
The above is a gradient, produced by the MSIE 3.0 gradient control.
</BODY>
</HTML>

Here is an explanation of the parameters used here:

A Sampling of ActiveX Controls and Features

It would be difficult to cover all the available ActiveX controls, because there are so many-and more are being developed daily. In this section, you'll take a look at some of the controls available from Microsoft. This should give you an idea of the types of things that are possible with ActiveX:

Figure 18.4 : The ActiveX Chart control enables you to display graphs and charts within Web pages.

Note
All the controls listed are available from Microsoft's Web site. See appendix C, "Online JavaScript Resources," for a listing of other pages with downloadable controls.

As you can see, just about anything can be done with ActiveX controls. There's a price, however-some of the controls can be quite slow, and downloading a new control can be even slower. You'll also have to consider whether you want non-Windows users to be able to view your page.

Using the ActiveX Control Pad

You don't have to understand ActiveX-or even HTML-to use an ActiveX control in a Web page of your own. Microsoft has made it easy with a product called the ActiveX Control Pad. This includes a text editor for the HTML page, as well as dialogs to control the properties of ActiveX controls.

To find out more about the ActiveX Control Pad and download it, see this page at Microsoft's Web site:

http://www.microsoft.com/workshop/author/cpad/tutorial-f.htm

Here's what you'll need to use the ActiveX Control Pad:

Once you install the control pad, you can begin to use it. Note that it's best to be online while using it so that it can access ActiveX control definitions if necessary.

The main screen includes a text editor; you can load any HTML page into this window. You can use the Insert ActiveX Control command from the Edit menu to insert a control. If you already have an existing control in the page, an icon will be shown next to the <OBJECT> tag.

Clicking a control's icon shows the control in action, along with a dialog that lists the properties for the control and enables you to change them. This is shown in Figure 18.5.

Figure 18.5 : The ActiveX Control Pad shows an HTML document, ActiveX control, and properties list.

Once you accept the properties you have selected, the appropriate tags are inserted into the HTML document to set those parameters for the control. It's that simple.

Note
The ActiveX Control Pad can be used with any ActiveX control. However, you must be licensed to use the control. Many controls can be used freely; with others, you may have to purchase a license to use them in your own pages.

Developing ActiveX Controls

Anyone can develop ActiveX controls, although it's not as easy as JavaScript. If you've programmed in C++ before, you will find it easy. Here's what you need to develop your own ActiveX controls:

For more information about ActiveX and the SDK, see Microsoft's ActiveX information at this URL:

http://www.microsoft.com/intdev/sdk/

An Introduction to VBScript

One of the latest weapons in Microsoft's war with Netscape is VBScript. VBScript is meant to compete directly with JavaScript, and it includes many of the same features.

You might have noticed that this book takes 17 chapters to explain JavaScript-of course, VBScript can't be fully explained in one chapter. This section presents the basics of VBScript and shows you how to create a simple VBScript application. You'll also look at some of the key differences between JavaScript and VBScript.

Note
To learn more about VBScript, consult Microsoft's online documentation or get a copy of Teach Yourself VBScript in 21 Days by Brophy and Koets, published by Sams.net. appendix D of this book includes a list of online references to get you started.

Creating a Simple VBScript Application

Like JavaScript, VBScript is included directly in an HTML file, and it is delimited with the <SCRIPT> tag. A script tag for a VBScript application looks like this:

<SCRIPT LANGUAGE="VBScript">

The actual language is a bit less complex in syntax than JavaScript; there is less punctuation, and many commands are simpler. Like JavaScript, VBScript can have functions, statements, and event handlers.

Because VBScript is a subset of Visual Basic, you may find it easy to learn if you've used Visual Basic before. However, there are many differences, so don't assume you already know everything.

As an introduction to VBScript, Listing 18.2 shows a simple VBScript application. The page includes a button; pressing the button causes an alert box (or message box, in VBScript lingo) to be displayed.


Listing 18.2. (VBSCRIPT.htm) A simple VBScript application.
<HTML>
<HEAD><TITLE>A simple VBScript program</TITLE>
</HEAD>
<BODY>
<H1>VBScript Example</H1>
<hr>
The button below triggers a VBScript function which displays
an alert box.
<hr>
<INPUT TYPE=BUTTON VALUE="Test Button" NAME="Button1">
<SCRIPT LANGUAGE="VBScript">
Sub Button1_OnClick
MsgBox "You pressed the button.", 0, "Window title."
End Sub
</SCRIPT>
<hr>
End of VBScript function
</BODY>
</HTML>

Figure 18.6 shows this program in action, with the message box displayed. Notice that rather than using an event handler attribute on the button tag, the procedure's name specifies the button's name and the event handler name.

Figure 18.6 : The VBScript example in action.

VBScript Versus JavaScript

Finally, let's take a quick look at some of the key features of JavaScript and how VBScript compares. This will give you some idea of what you're in for if you decide to learn VBScript as your next Web language.

Punctuation and Syntax

An obvious difference between JavaScript and VBScript is in its use of basic punctuation and syntax. VBScript does not use semicolons or brackets at all. Function declarations and other blocks of code, such as loops, are enclosed with beginning and ending statements. For example, here is a procedure definition:

Sub Message(text)
  MsgBox text
End Sub

This syntax is used for a procedure-a function that doesn't return a value. The Function keyword is used for functions that do return a value.

Variables

Like JavaScript, VBScript is a loosely typed language. There is one variable type, called Variant, that can hold a number, a string, or other types of values. Variables must be declared with the Dim statement (similar to Var in JavaScript). This example declares a variable and assigns a string value to it:

Dim Answer
Answer = "The answer is 42."

VBScript and Forms

Like JavaScript, VBScript can access all the elements in a form-in fact, the syntax is strikingly similar. This is the VBScript syntax to refer to the text1 input field in the form1 form:

val = Document.form1.text1.value

VBScript can be used for form validation and can prevent form submission while fields are not filled in correctly.

Object-Oriented Features

Although VBScript can access form elements and ActiveX controls as objects, it is not as object-oriented as JavaScript. There is currently no way to create new objects or add properties to existing ones. These may be added in a future version.

ActiveX and VBScript

VBScript can access and control ActiveX controls, similar to the way JavaScript can work with Java. Each ActiveX control is available as an object to scripts in that page. Objects can generate events, and they can call VBScript functions when the events occur.

Workshop Wrap-Up

In this chapter, you learned about Microsoft Internet Explorer, ActiveX, and VBScript:

You also explored a sampling of the available ActiveX controls.

Next Steps

Move on with one of the following:

Q&A

Q:
If MSIE supports JavaScript and Netscape will support VBScript, which language will be the standard?
A:
Honestly, it's anyone's guess. Of course, Netscape wants JavaScript to be the standard, and Microsoft wants VBScript to be the standard. However, on the Web right now there are many sites using JavaScript and very few using VBScript. Will this change? Who knows.
Q:
Can VBScript be used with Java, as JavaScript can?
A:
Yes, according to Microsoft. The Java support in the latest MSIE is still in beta, though, and I haven't seen a working example of this yet.
Q:
Which language should I use in my Web pages?
A:
Because both MSIE and Netscape support it, at least to a point, JavaScript seems the clear choice right now. JavaScript is also more sophisticated than VBScript. Although MSIE is gaining rapidly, Netscape is still used by about 90 percent of Web users at this writing.