interactive.gif (47998 bytes)

4. Contemporary Web Design Standards

Why scriptlets over Active X or Java?

One reason for Visual Basic's enormous popularity is the component model it has pioneered. For building small to medium-size GUI applications, it yields productivity improvements and ease of use that programmers raised on Charles Petzold's classic Programming Windows still find hard to believe. Because a sophisticated Web-based application is essentially a small to medium-size GUI application, the obvious question is how to bring the component model of Visual Basic and Delphi to Web development. Essentially, there have been two approaches: ActiveX Controls (the same controls used so successfully in the current versions of VB and Delphi), and Java applets.

Except on Intel-based Intranets, ActiveX controls have essentially been nonstarters. In spite of some attempts at extending them to other platforms, they remain in essence binary code that's standard for Win32 platforms. Their security model is also weaker than Java applets'. ActiveX controls support only code signing, rather than the three-prong approach supported by Java--virtual machine, security manager, and signing.

Java applets (especially when recast as JavaBeans) show more promise. Still, the number of sites using Java is surprisingly small, and many corporate sites refuse to allow downloaded Java applets to be run for fear of some as yet undiscovered security bug. In addition, Java is not the easiest language in the world for Web developers to learn, and a critical mass of JavaBeans has not yet appeared.

What Is a Scriptlet?

It is hard to know how an idea sees the light of day, but clearly some folks at Microsoft had the insight that DHTML pages have at least at one level exactly the same features as Visual Basic controls, including:

            *properties to describe how the pages look and feel;

            *methods to control how they behave;

            *events that the objects can be programmed to respond to.

From that point, it must have been no great leap to decide to build a reusable object technology by allowing DHTML pages to be treated as self-contained objects. Thus a scriptlet is a complete Web-ready HTML page that includes information that allows you to work with it as a control: You can get and set its properties, call its methods, and so on. More precisely, we might say that a scriptlet is a Web page based on DHTML that can be reused as a component with any application that supports such components.

Scriptlet Pros and Cons

The advantages to using scriptlets are:

*It is much easier to learn DHTML and scripting than Java.
*DHTML pages can be reused.
*Scriptlets have excellent performance; they're as fast as DHTML.
*Scriptlets are lightweight, like any other page, with minimal overhead.
*Scriptlets may eventually be truly cross-platform, with no virtual machine compatibility problem
*Scriptlets are as secure as DHTML. Like Java applets, scriptlets must be loaded from the same Web servers as their container pages.
 
The disadvantages are:

*Scriptlets are not yet truly cross-platform. There is no guarantee that Netscape will adopt scriptle technology. Microsoft's support, currently available only in Internet Explorer 4, depends on the company's Component Object Model (COM), which is not a cross-platform technology.

 *A programmer working strictly in DHTML is limited, compared with what a skilled programmer can do in a full-fledged programming language such as Java. (But a DHTML programmer can do a lot; for example, you could build a "nervous text" scriptlet that works exactly like the famous Java applet of that name.)

*There is no way to keep a scriptlet's code hidden.

Scriptlet Mechanics

Any DHTML page can be embedded as a scriptlet via a new MIME type that Microsoft has submitted to W3. You use it in the <OBJECT> tag, as in the following prototype:

            <object type="text/x-scriptlet"

            data= "ScriptletUrl
            .html">

            </object>

The scriptlet is named in the data parameter in the form of a standard URL. Notice that unlike ActiveX controls, there is no inscrutable CLASSID in the scriptlet object tag. As with any use of the <OBJECT> tag, you can also pass parameters to the scriptlet with tags like these:

            <param name="ImageName" value="
            PCMagLogo1.jpg"> <param name="Speed"
            value="100">

Like Visual Basic controls, scriptlets need to expose properties, methods, and events to the users of the component. When you build a scriptlet using VBScript, the coding style is very close to the way you build an ActiveX control in VB5.

Let's start with methods. They are simply the functions or procedures that begin with a public_prefix. (Case, as with any VBScript code, is irrelevant.) For example, the signature for the following could give a public Start method to an
animation scriptlet:

            Sub public_Start()

            'code goes here

            End Sub

Any functions or procedures not prefixed with public_   are private to your scriptlet.

Properties use two prefixes in pairs: public_get_ and public_put_ (equivalent to VB5's Property Get/Let) If you leave out the corresponding public_put to a public_get, you have a read-only property. For example:

            Sub public_put_AnimationSpeed(Speed)

gives you a way to set an animation's speed property. (VBScript, unlike JavaScript, maintains a distinction between Sub routines, which don't return a value, and functions, which do.)

Following standard object-oriented programming principles, you can use private instance fields (data members) to encapsulate the property values. It is true that a variable with the public_ prefix is the equivalent of a read/write property, but most programmers would discourage that formulation, preferring to permit access to private data fields only through explicit member functions.

Note that when you refer to a property or method of the scriptlet from a page that uses it, you do not include the prefix. For example:

Animation Scriptlet .AnimationSpeed = Speed

is how your code using the scriptlet would look.

Important Events for Scripting
 
 

Important Events for Scripting
Event Generated When
OnMouseOver Mouse pointer moves to the element. 
OnMouseOut Mouse moves out of the element. 
OnMouseDown Mouse button pressed inside the element. 
OnMouseUp Mouse button released inside the element. 
OnMouseMove Mouse pointer moves in inside the element. 
OnClick Left mouse button is clicked on the element. 
OnDblClick Left mouse button is double-clicked on the element. 
OnKeyPress Key is pressed and released. Holding a key down triggers multiple events. 
OnKeyDown Key is pressed . Triggers one event no matter how long the key is held down.
OnKeyUp Key is released.

 


yellowsphere3.gif (1033 bytes)Go to Topic 5