Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Part IV
Java Script

CHAPTER 18
Introduction to JavaScripting

by Jim O'Donnell

In this chapter
Introduction to JavaScript 440
The JavaScript Language 443
Programming with JavaScript 445
JavaScript and Web Browsers 458

Introduction to JavaScript

JavaScript enables you to embed commands in an HTML page. When a compatible Web browser, such as Netscape Navigator 2 or higher or Internet Explorer 3 or higher, downloads the page, your JavaScript commands are loaded by the Web browser as a part of the HTML document. These commands can be triggered when the user clicks page items, manipulates gadgets and fields in an HTML form, or moves through the page history list.


NOTE:  Microsoft’s Internet Explorer Web browser supports JScript, Microsoft’s own implementation of Netscape’s JavaScript language. JScript is compatible with JavaScript, but some differences exist. When programming in JavaScript, it is always a good idea to test your scripts by using both browsers.

Some computer languages are compiled—you run your program through a compiler, which performs a one-time translation of the human-readable program into a binary that the computer can execute. JavaScript is an interpreted language—the computer must evaluate the program every time it is run. You embed your JavaScript commands within an HTML page, and any browser that supports JavaScript can interpret the commands and act on them.

JavaScript is powerful and simple. If you’ve ever programmed in C++ or Java, you will find JavaScript easy to pick up. If not, don’t worry. This chapter will have you working with JavaScript in no time. This chapter examines the basic syntax of the language, and the other chapters in this section show you how to apply it. In Chapter 19, “The Web Browser Object Model,” you will find out how to use JavaScript to interact with your Web pages and how to build it in to your HTML. Chapters 20 through 23 then show you in greater detail how to use JavaScript with windows and frames, HTML forms, and Web browser cookies, and how to control other Web browser objects—such as Java applets and ActiveX Controls—with JavaScript.

Why Use a Scripting Language?

HTML provides a good deal of flexibility to page authors, but HTML by itself is static; after being written, HTML documents can’t interact with the user other than by presenting hyperlinks. Creative use of CGI scripts (which run on Web servers) and the newer Web technologies, such as Java, ActiveX Controls, and Dynamic HTML, have made it possible to create more interesting and effective interactive sites. Even so, a scripting language is very often what ties all the elements of a Web page together.

JavaScript enables Web authors to write small scripts that execute on the users’ browsers rather than on the server. An application that collects data from a form and then posts it to the server can validate the data for completeness and correctness, for example, before sending it to the server. This can greatly improve the performance of the browsing session because users don’t have to send data to the server until it has been verified as correct.

Another important use of Web browser scripting languages such as JavaScript comes as a result of the increased functionality being introduced for Web browsers in the form of Java applets, plug-ins, Dynamic HTML elements, ActiveX Controls, and VRML objects and worlds. Web authors can use each of these things to add extra functions and interactivity to a Web page. Scripting languages act as the glue that binds everything together. A Web page might use an HTML form to get some user input and then set a parameter for a Java applet based on that input. It is usually a script that carries this out.

What Can JavaScript Do?

JavaScript provides a fairly complete set of built-in functions and commands, enabling you to perform math calculations, manipulate strings, play sounds, open new windows and new URLs, and access and verify user input to your Web forms.

Code to perform these actions can be embedded in a page and executed when the page is loaded. You can also write functions containing code that is triggered by events you specify. You can write a JavaScript method that is called when the user clicks the Submit button of a form, for example, or one that is activated when the user clicks a hyperlink on the active page.

JavaScript can also set the attributes, or properties, of Web page elements, ActiveX Controls, Java applets, and other objects present in the browser. This way, you can change the behavior of plug-ins or other objects without having to rewrite them. Your JavaScript code could automatically set the text of an ActiveX Label Control, for example, based on what time the page is viewed.

What Does JavaScript Look Like?

JavaScript commands are embedded in your HTML documents. Embedding JavaScript in your pages requires only one new HTML element: <SCRIPT> and </SCRIPT>. The <SCRIPT> element takes the attributes LANGUAGE, which specifies the scripting language to use when evaluating the script, and SRC, which can be used to load a script from an external source.

JavaScript itself resembles many other computer languages. If you are familiar with C, C++, Pascal, HyperTalk, Visual Basic, or dBASE, you will recognize the similarities. If not, don’t worry—the following are some simple rules to help you understand how the language is structured:

  JavaScript is case sensitive.
  JavaScript is flexible about statements. A single statement can cover multiple lines, and you can put multiple short statements on a single line—just make sure to add a semicolon (;) at the end of each statement.
  Braces (the { and } characters) group statements into blocks; a block may be the body of a function or a section of code that gets executed in a loop or as part of a conditional test.


NOTE:  If you program in Java, C, or C++, you might be puzzled when looking at JavaScript programs—sometimes each line ends with a semicolon, sometimes not. In JavaScript, unlike those other languages, the semicolon is not required at the end of each line.

JavaScript Programming Conventions

Even though JavaScript is a simple language, it is quite expressive. This section reviews a small number of simple rules and conventions that will ease your learning process and speed your use of JavaScript.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.