Client-Side and Server-Side Scripting

Implementing Client-Side Scripting

To use client-side scripting to enhance the functionality of a Web page, you embed the source code into its HTML page as ASCII text. When it encounters a script, the Web browser calls a scripting interpreter, which parses and deciphers the scripting code. The Web browser must support scripting, and it must be able to integrate scripting with ActiveX controls or Java applets embedded in the HTML page. If a user's Web browser does not support the scripting language, the user will not have full access to the features in your Web page.

Scripting Languages

Any scripting language can be used to develop Web applications as long as there is an interpreter that supports the language. However, Microsoft VBScript and Microsoft JavaScript are the most common scripting languages.

VBScript

Microsoft Visual Basic Scripting Edition (VBScript) is a case-insensitive subset of the Microsoft Visual Basic language that is upwardly compatible with Visual Basic for Applications. VBScript is supported by Microsoft Internet Explorer. The VBScript Interpreter is fast, portable, and can be freely licensed from Microsoft. Because Visual Basic is a widely known language with thousands of developers worldwide, the learning curve for VBScript is very short.

JavaScript

JavaScript is implemented by Microsoft as JScript. It is a C-like language that is based on Java, a programming language developed by Sun Microsystems and Netscape. JavaScript is supported by Netscape Navigator and Internet Explorer.
Note

To aid your development efforts, Microsoft has provided a tool, called the Microsoft Script Debugger, as a free add-on for Microsoft Internet Explorer. The Script Debugger provides debugging features for Web applications and works with both JavaScript and VBScript.


Choosing a Scripting Language

VBScript and JavaScript are similar languages. In both languages, you can define variables , create procedures, and access properties and methods of objects.

Only subtle variations in syntax define the difference between the two languages. Neither is compiled, and both will run on any hardware platform. Both languages are interpreted, so speed differences are a result of the Web browser and not of the language itself.

When choosing a scripting language, consider the following issues:

  • Web browser compatibility

    The user's Web browser must include a scripting interpreter for the scripting language you use. Microsoft Internet Explorer has interpreters for both VBScript and JavaScript. Netscape Navigator provides an interpreter for JavaScript. Users can acquire a Netscape plug-in to support VBScript.

  • Programmer familiarity

    Use the scripting language that you are most familiar with. If you have Visual Basic experience, you can quickly learn VBScript. If you have Java or C experience, JavaScript will be more familiar to you.

The <SCRIPT> Tag

Scripting code is contained within the HTML <SCRIPT> tag. The LANGUAGE parameter tells the Web browser which interpreter to use when running the code. For VBScript, LANGUAGE is "VBScript"; for JavaScript, LANGUAGE is "JavaScript." You must specify the language because some Web browsers, such as Internet Explorer, can use more than one scripting language.
Note

You can use VBScript and JavaScript together on the same page by setting the LANGUAGE parameter.


Example

This example, written in VBScript, runs when the user clicks the corresponding button in the Web page:
 <SCRIPT LANGUAGE="VBScript"> <!-- Sub BtnHello_OnClick() MsgBox "Hello, world!" End Sub --> </SCRIPT> 

Note

Web browsers that do not understand scripting code display the code in the HTML page as regular text. To prevent this, place scripting code in comment tags (<!-- and -->).


When the Web browser gets to the <SCRIPT> tag, it calls the VBScript interpreter to compile the code. In the preceding example, the code is associated with the Click event of a button named btnHello and the output is written in a message box.

Example

This example defines a general function that is called by other procedures within the Web page. The quantity ordered ( txtQty ) is passed to the function. If the quantity exceeds 100 units, a discount is applied to the order:
 <SCRIPT LANGUAGE="VBScript">  <!-- Function Discount(txtQty) If txtQty > 100 then Discount = .10 End Function --> </SCRIPT> 

When writing scripting code, place all of your code within the same <SCRIPT> tag. Although your HTML page can contain more than one <SCRIPT> tag, the code is easier to maintain when it is located in the same tag. You can place the <SCRIPT> tag in either the BODY or the HEAD sections of the HTML page.

Lesson Summary

Script is code used to programmatically manipulate Web pages. Client-side script runs on the client computer, and server-side script runs on a Web server before the server returns the page to a user. For client-side script to function, a Web browser must support the scripting language. Server-side script runs regardless of which Web browser is used.

To use client-side scripting, you include the source code within an HTML document. When a Web browser encounters script within the HTML, it calls a scripting interpreter, which parses and deciphers the scripting code.

Microsoft Visual Basic Scripting Edition (VBScript) and JavaScript are two common scripting languages. VBScript is a subset of the Microsoft Visual Basic language, and is supported by Microsoft Internet Explorer.



Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

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