Export (0) Print
Expand All

JavaScript

Perhaps the best-known scripting language prior to the introduction of VBScript was JavaScript. JavaScript is used to create interactive web applications supported by the Netscape browser. JavaScript offers many of the same advantages as VBScript. JavaScript is simple to use, lightweight, and dynamic. Developers can easily embed code functionality for interactive applications inside a web page.

The most noticeable difference between JavaScript and VBScript is the syntax. The syntax for JavaScript is similar to the syntax for the C++ programming language. Since VBScript is a subset of Visual Basic for Applications, VBScript follows the Visual Basic for Applications syntax.

Consider the "Hello, World!" VBScript example presented in Chapter 1. How much would the HTML change if we wanted to use JavaScript instead of VBScript to accomplish the same functionality? Listing 2-1 shows the complete example, but the key differences are in the scripting syntax. (Note that Listing 2-1 and all other full program listings are available on this book's companion CD, in the samples folder.) The JavaScript portion of the code looks like this:

<SCRIPT LANGUAGE="JavaScript">
    var rtn=false
    function push_me() {
        alert("Hello, World!")
        return true
    }
</SCRIPT> 

Notice the syntax's similarity to C++. Curly braces are used to surround functions that return values based on success or failure. Although not overly complex, the syntax is obviously different from VBScript.

Listing 2-1

<HTML>
<HEAD>
<TITLE>My First JavaScript Application</TITLE>

<SCRIPT LANGUAGE="JavaScript">
    var rtn=false
    function push_me() {
        alert("Hello, World!")
        return true
    }
</SCRIPT> 

</HEAD>
<BODY>
<CENTER>

<FORM>
<INPUT TYPE = "BUTTON" VALUE = "Push Me!"
NAME = "Command1" OnClick="rtn=push_me()">
</FORM>

</CENTER>
</BODY>
</HTML> 

The Internet Explorer 3.0 supports JavaScript directly through a scripting engine in the file jscript.dll. In fact, you can use both VBScript and JavaScript in a single web page. Listing 2-2 shows an example of utilizing JavaScript and VBScript in the same page. Figure 2-2 shows a sample output. In the Internet Explorer 3.0, both JavaScript and VBScript can read and modify HTML form elements, interact with the browser, and automate any ActiveX component.

Listing 2-2.

JavaScript and VBScript.

<HTML>
<HEAD>
<TITLE>JavaScript and VBScript</TITLE>

<!--
This sample compares two functions that
use a text box to display a message.
Note the key differences between VBScript
and JavaScript.
-->

<SCRIPT LANGUAGE="JavaScript">
    var rtn=false
    function push_me() {

        // This function uses a literal string
        // and a property of the text box. Note
        // that JavaScript can set the values
        // for the variables when they are
        // declared with var. 
        var msg1="This is a JavaScript message: "
        var msg2=document.frmScript.txtMessage.value
        alert(msg1 + msg2)
        return true
    }
</SCRIPT>

<SCRIPT LANGUAGE="VBScript">
<!--
    Option Explicit
    Sub cmdVBS_OnClick

        ' This is a VBScript version of the
        ' JavaScript function.  VBScript must set
        ' and declare variables on separate
        ' lines.

        Dim MyForm
        Set MyForm=Document.frmScript
        Dim msg1,msg2        
        msg1="This is a VBScript message: "
        msg2=MyForm.txtMessage.Value
        MsgBox msg1 & msg2
    End Sub
-->
</SCRIPT>

</HEAD>
<BODY>
<CENTER>

<FORM NAME="frmScript">
<INPUT TYPE="TEXT" NAME="txtMessage" VALUE="Test">
<P>
<INPUT TYPE="BUTTON" VALUE="JavaScript Message"
NAME="cmdJava" OnClick="rtn=push_me()">
<P>
<INPUT TYPE="BUTTON" VALUE="VBScript Message"
NAME="cmdVBS">
</FORM>

</CENTER>
</BODY>
</HTML> 

Figure 2-2.

Sample output of an HTML page that uses both JavaScript and VBScript.

VBScript

VBScript is a subset of Visual Basic for Applications. Therefore, VBScript programming has many similarities to Visual Basic for Applications programming. Many of the powerful features of Visual Basic for Applications, such as classes and API calls, were omitted to make the language portable and secure.

Although VBScript is just text and can be written with a simple text editor, a graphical design tool for VBScript is available. This visual layout tool is called ActiveX Control Pad. ActiveX Control Pad allows you to combine HTML code, ActiveX controls, HTML layouts, and VBScript or JavaScript. ActiveX Control Pad works in conjunction with the HTML Layout control. The HTML Layout control is a drawing board that allows you to visually add and manipulate controls. ActiveX Control Pad will be discussed in detail later in this book.

VBScript promises the same boom in ActiveX development that we have seen in OCX controls development. Because VBScript, with the support of the Internet Explorer, can automate ActiveX components, vendors can design ActiveX controls to perform particular Internet tasks. A stock ticker control is a good example of such a control. Admittedly, we already have a rich assortment of ActiveX controls in the form of OCX controls, but these controls are typically not optimized for downloading across the Internet. For example, many of these controls contain sizable design-time components that cause long delays during a download.

VBScript does not have an integrated debugging environment (IDE). This means that code debugging can be rather demanding. Typically, you run the script in a browser and troubleshoot any errors that may occur. Perhaps the most useful debugging component is a well-placed MsgBox, and until a complete IDE is available, development will remain cumbersome. The obvious need for an IDE is overwhelming, and Microsoft certainly knows that programmers want one; however, releasing VBScript "as is" allows developers to get a handle on the new concepts without waiting for a more formal product. After coding a few pages directly, you will quickly master any IDE that is delivered in the future.

Compatibility with existing browsers is another issue for VBScript. Although the Internet Explorer supports both VBScript and JavaScript, VBScript is not currently supported by the Netscape browser. In the future, we may see support from Netscape and others, but for now, interactive web sites that use VBScript are limited to platforms running the Internet Explorer.

In a bid to create standard scripting support that would broaden the appeal of VBScript while maintaining current support for other engines, such as JavaScript, Microsoft is working with the World Wide Web Consortium to develop a standard for ActiveX Scripting and the event-driven model. Such efforts will improve the viability of VBScript, but even without an absolute standard, the Internet Explorer promises to have a large market share because of tight integration with future releases of Windows. Ultimately, VBScript may take center stage in Windows development as the browser becomes the primary desktop interface. Work is ongoing at Microsoft to completely unify the desktop environment inside the browser, creating a single, comprehensive view of local files, network files, and Internet files. This new view is known as page view.

Implementing page view will radically alter the Windows desktop environment. First and foremost, the concept of a separate browser running on a desktop is eliminated. In fact, the browser will be the desktop. Imagine an environment in which the Windows Explorer is replaced by a browser interface. You will view all of the files on your local machine just as if they are web pages. Need to see a Word document? View it in a browser. Need to work on a Microsoft Excel spreadsheet? Open it in the browser.

Ultimately, the user will work with all data the same way, regardless of the location of the data. For example, local, network, and Internet files will be seamlessly integrated into the browser interface. This represents a significant advance in the user interface because users will no longer have to worry about data location and the technical knowledge necessary to connect to the data.

JavaScript Versus VBScript

JavaScript and VBScript have many similarities. In fact, anyone who has mastered VBScript will find JavaScript just as easy to learn. Since the Internet Explorer supports both languages, knowledge of the fundamentals of both is valuable. The key differences between JavaScript and VBScript are covered in the paragraphs that follow.

Syntax

As stated before, the most obvious difference is syntax. JavaScript uses curly braces to denote functions, whereas VBScript uses Function…End Function and Sub…End Sub. In fact, JavaScript supports only functions, whereas VBScript supports functions and subroutines. If you want to create a function that behaves like a subroutine in JavaScript, simply omit the return value.

Objects

Neither JavaScript nor VBScript is truly object-oriented. Without digressing into a full definition of object-oriented programming, we can safely say that neither language exhibits all of the characteristics of a truly object-oriented language. Neither language, for example, supports the concept of inheritance.

JavaScript, however, makes stronger use of objects than does VBScript. JavaScript allows for the definition of classes for the subsequent creation of objects. To define a class, you create a function that specifies the class name and the class's properties and methods. For example, the following code defines a class called Student that has three properties:

function Student (name, height, weight) {
    this.name=name
    this.height=height
    this.weight=weight
} 

Values are assigned to properties using the this keyword, which always references the new object. This type of design allows JavaScript to encapsulate properties and methods inside an object. Creating an instance of the Student class is a simple matter of calling the function and using the new keyword:

mystudent=new Student("John",70,175) 

Unfortunately, VBScript does not support object creation or user-defined classes. VBScript supports only reusable functions and subroutines. In order to use VBScript to manage the above information for multiple students, we would have to create separate variables for each student we wanted to track. We could accomplish this by using arrays, for example.

Language Scalability

A primary difference between VBScript and JavaScript is scalability. Once you learn VBScript, you are well on your way to learning Visual Basic for Applications. If you know Visual Basic or Visual Basic for Applications, you know VBScript. Although JavaScript has similarities to C++, it is a new language.

Other Scripting Languages

Because Microsoft has designed ActiveX Scripting to be an open standard, JavaScript and VBScript may soon be joined by other scripting languages. The Internet Explorer can support any scripting language that takes advantage of ActiveX Scripting. Third-party vendors may choose to design their own scripting languages.

The VBScript Language

In order to understand the VBScript language, you must first understand what's missing. VBScript is not a complete, mature development language. Many features that are a necessity in advanced languages, such as data types, are not supported in VBScript. The language is a simple collection of fundamental features taken right out of the Visual Basic for Applications language. If you have programmed with Visual Basic for Applications before, you will find VBScript familiar. Table 2-1 shows the features of the VBScript language. Appendix C shows the Visual Basic for Applications features that are not supported by VBScript. For more information, consult the VBScript documentation on this book's companion CD and at Microsoft's web site.

Table 2-1.The VBScript Language

Category Keywords
Array handling Array function

Declaration (Dim, Static, etc.)

Lbound, Ubound

ReDim, Erase

Assignment =, Let, Set
Comments REM and '
Constants/Literals Empty, Nothing, Null

True, False

User-defined literals

Control flow Do…Loop

For…Next, For Each...Next

While…Wend

If…Then…Else

Select…Case

Conversion Abs

Asc, Chr

CBool, CByte

CDate, CDbl, CInt

CLng, CSng, CStr

CVErr

Fix, Int, Sgn

Hex, Oct

Date/Time Date function, Time function

Day, Month, Weekday, Year

Hour, Minute, Second

Now

DateSerial, DateValue

TimeSerial, TimeValue

Error handling On Error Resume Next

Err Object

Input/Output InputBox

MsgBox

Math Atn, Cos, Sin, Tan

Exp, Log, Sqr

Randomize, Rnd

Miscellaneous Line continuation character (_)

Line separation character (:)

Objects Create Object
Operators +, -, *, /, ^, Mod

Integer Division (\)

Negation (-)

String concatenation (&)

=, <>, <, >, <=, >=, Is

Not, And, Or, Xor

Eqv, Imp

Options Option Explicit
Procedures Function, Sub

Exit Function, Exit Sub

Call

ByVal, ByRef

Strings Asc, AscB, AscW

Chr, ChrB, ChrW

Instr, InStrB

Len, LenB

LCase, UCase

Left, LeftB

Mid, MidB

Right, RightB

Space(number)

StrComp

String(number, character)

Trim, LTrim, RTrim

Variables Procedure-level:

Dim, Static

Module-level:

Private, Dim

Variants IsArray

IsDate, IsEmpty, IsError

IsNull, IsNumeric, IsObject

VarType

© 1996 by Scot Hillier. All rights reserved.

Show:
© 2014 Microsoft