Client-Side Scripting

In an early effort to increase the interactivity of HTML Web pages, some developers turned to scripting, adding code-based functionality by combining a programming language with HTML. This approach often results in a strange hybrid of code and tags that once again takes developers back to the text editor. In scripting, the <SCRIPT> tag is introduced to define a code section in the Web page. Listing 1-3 uses VBScript to create a "Hello, World!" example, as shown in Figure 1-3.

Listing 1-3. VBScript code for "Hello, World!"


<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Developer Studio"> <META HTTP-EQUIV="Content-Type" content="text/html;      charset=iso-8859-1"> <TITLE>Yet Another Hello, World! Example</TITLE> <SCRIPT LANGUAGE="VBScript"> <!--     Sub cmdClickMe_OnClick()         MsgBox "Hello, World!"     End Sub --> </SCRIPT> </HEAD> <BODY BGCOLOR="WHITE"> <FORM> <INPUT TYPE="BUTTON" NAME="cmdClickMe" VALUE="Click Me!"> </FORM> </BODY> </HTML> 

Figure 1-3. "Hello, World!" created using VBScript.

VBScript is a scripting language based on Microsoft's popular Visual Basic for Applications (VBA), a language found in products such as Microsoft's Office 97. VBScript is not a complete version of VBA but rather a subset that keeps many of the key features of VBA while removing features that might make the language unnecessarily bulky or unsafe. For example, VBScript does not support data types—every variable defined is a Variant.

Like its big brother, VBA, VBScript is an event-driven language. This means that the code you write is executed in response to an event, a user interaction with a graphical user interface, or GUI. For our purposes, the GUI is the Web page. So, in the example above, when the user interacts with the GUI by clicking the Click Me! button, the action fires the OnClick event. This event is mapped to the VBScript code by way of an event-handling subroutine. A subroutine is named ControlName_EventName for any control and event combination. The only caveat here is that some events can also be generated by the browser itself. For example, when a Web page completes a load into the browser, the Window_OnLoad event fires, independent of any user interaction with the page.

Although scripting represents an advancement in interactivity, it has its limitations. For example, not all browsers recognize scripting. Among those that do, not all use the same language. Chief among these is Netscape Navigator 3.0, which does not recognize VBScript but instead recognizes JavaScript, a scripting language originally created for Netscape Navigator. JavaScript is similar in function to VBScript but is very different in syntax. Unlike VBScript, JavaScript does not support the concept of an event-handling subroutine. All routines in JavaScript are functions, which are called by virtue of event attributes that reside in the HTML tag. Listing 1-4 is the JavaScript version of the previous VBScript example.

Listing 1-4. JavaScript code for "Hello, World!"


<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Developer Studio"> <META HTTP-EQUIV="Content-Type" content="text/html;      charset=iso-8859-1"> <TITLE>JavaScript Hello, World! Example</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!--     function clickme()     {         alert("Hello, World!");         return true;     } --> </SCRIPT> </HEAD> <BODY BGCOLOR="WHITE"> <FORM> <INPUT TYPE="BUTTON" NAME="cmdClickMe"     VALUE="Click Me!" OnClick="var rtn=clickme();"> </FORM> </BODY> </HTML> 

Not only does support for scripting vary by browser, but scripting does not provide the mature functionality programmers expect in a language. Scripting offers a subset of the language features developers normally use, a subset that limits structures and operators to fundamental looping and decision making. In fact, scripting alone is generally useful only for performing client-side data validation prior to submitting the form to a server.

Once scripting is added to the mix, platform complications abound. Obviously, if a scripting language is not universal, it cannot be adequately deployed on the Internet, and consequently all of the advertised advantages of platform independence on the Web come to nothing. For many Webmasters and developers, the constant battle between browsers for market share has resulted in the need to maintain three versions of each Web site: one for Microsoft Internet Explorer, one for Netscape Navigator, and one for old browsers such as Mosaic that can't understand any of the new technology.



Programming Active Server Pages
Programming Active Server Pages (Microsoft Programming Series)
ISBN: 1572317000
EAN: 2147483647
Year: 1996
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net