10 Jan 1997

Browser JavaScript and VB Script

Web pages can contain simple program logic in the form of embedded scripts. Netscape created a portable script language supported by the Navigator Browser called "JavaScript". Microsoft's IE Browser also supports JavaScript, but it also supports Visual Basic script and can be configured to call other languages.

A script program can set the defaults and check the values of HTML form fields. For example, if a Web page offers to sell computer supplies, and normal delivery is 5 days, then a Web page shipped out to the Browser on Monday Jan 13 might contain a script that fills in the promised delivery date as Friday Jan 17. To make the delivery date sooner, the user would have to select a more expensive shipping option.

The same thing can be done with Server programming, but that is a slower and more expensive option. Browser scripts can detect errors as they are typed, popping up an immediate helpful error message. They can also route a form to different Servers or programs based on the data entered by the user.

Script Languages

A "script" is a simple computer program that the computer executes by interpreting the text source file. In DOS, the script language was found in *.BAT files. Unix has more powerful script languages for the Bourne or C shells. IBM mainframes typically used a script language named Rexx.

Netscape could find no existing script language that was available on all systems and had enough power to support Web pages but not so much power as to pose a security problem. So they created a new language called JavaScript and included it in their Browsers since Navigator 2.x.

Netscape has been refining the language in subsequent software releases. However, despite promises to make JavaScript a public standard, they waited a long time before releasing any formal definition or some code that could for a reference implementation. Microsoft needed to support something in Internet Explorer, so they created a "best guess" at what JavaScript is supposed to be and named the result JScript. Microsoft has no intentional differences, but the Microsoft and Netscape code may behave differently in areas where the language is ambiguous.

Netscape builds JavaScript support into the Navigator code. Microsoft created a public OLE interface between Internet Explorer and any script programming language that the user may choose to install. In addition to JScript, they also offer a version of its popular Visual Basic language named VB Script.

Other companies are adapting more traditional programming languages to the Microsoft OLE interface. There is a version of Perl under development. However, conventional programming languages support file I/O and other features that would be dangerous in a script downloaded from a foreign Web Server. For now, it is safest to concentrate on JavaScript and VB Script (or just JavaScript if it is important to support both Netscape and Microsoft clients).

What is the difference between JavaScript and VB Script? Well, JavaScript statements all end in a semicolon, while VB Script statements simply run to the end of the line. There are a few more details, but that is the big difference.

Many programmers are familiar with Visual Basic and may be confused about the new version. There are three members of the Visual Basic family:

Weakly Typed, Object-Based

Conventional programming languages, such as C, COBOL, or FORTRAN, are designed to perform complex calculations on large amounts of data efficiently. The programmer begins by defining the names of variables and specifying the type of data that each variable will hold. The compiler can then generate the correct instructions to perform any calculation.

Other languages, like Java, can be used to build large applications with many components. To detect errors as quickly as possible, they require that each variable be defined in advance to hold a particular type of data.

Script languages are used to build small, casual programs written by people who are not professionals. To make the code as simple as possible, variables do not have to be declared in advance, and they can be assigned either character strings or numbers.

Object Oriented programming languages, such as C++ and Java, combine blocks of data and packages of program logic into units called "objects". Programmers can create their own types of objects, or they can use objects defined by other programmers stored in libraries.

JavaScript and Visual Basic are Object-Based languages. Instead of creating new objects, the programmer is simply given access to the properties (the data) and the methods (the logic) of objects inherited from the environment. Traditionally, Visual Basic presented objects that represented elements of the graphic user interface (input fields, boxes, buttons, or lists). The same GUI objects also appear in a Web document as part of an HTML form.

Script languages become clients of objects created by more sophisticated conventional programming languages. The Web Browser and Web Server are written in C or C++. Third party code written to the Plug-In interface (Netscape) or ActiveX (Microsoft) extends the Browsers, just as code written to NSAPI (Netscape) or ISAPI (Microsoft) extends the Web Server. Both Netscape and Microsoft provide instructions on how such program extensions can generate new objects that will be visible to the scripting languages.

The set of standard objects generated by the Browser and the Server are entirely different. Browser objects involve Windows, Frames, Documents, Forms, and Fields. The Browser creates script objects that reflect the variable parts of the HTML that has processed. Server objects involve the Request (mostly the values filled into the form), Client (mostly information in or based on cookies), and Application. The Server also provides objects to allow its JavaScript programs to access disk files and databases (which a Browser JavaScript program cannot do).

Script Processing

A Browser script is embedded in a Web document. As the Browser scans the HTML, it extracts any script code and passes it on to the script language interpreter. There are three cases:

The Browser processes an HTML document from beginning to end. It formats text until it hits a <SCRIPT> tag. Then it passes the enclosed script text to the interpreter until it is done and ordinary text formatting can continue. Scripts cannot go back and replace already formatted text, unless the script clears the entire document and starts over again from the top. There is a strong bias, therefore, for script logic to make decisions early. It is a common convention to store the bulk of script code be stored in the <HEADER> section of a document, before any ordinary text has been formatted.

Although a script is embedded in an individual document, it can take a global view of everything that the browser is doing. When using frames, for example, a script in the "table of contents" document along the left side of the window can effect the processing that occurs in the main document window.

Continue Back PCLT

Copyright 1996 PC Lube and Tune -- Distributed Applications and the Web H. Gilbert