Complete Idiot's Guide to JavaScript

Complete Idiot's Guide to JavaScript

Complete Idiot's Guide to JavaScript, Second Edition

Part 1: What's It All About?

Chapter 1: Coffee? In My Computer? I Prefer Decaf... Chapter 2: Navigator and Explorer: The World Wide Web on a Caffeine High

Part 2: Let's Get Jivin'

Chapter 3: Tag...You're It! Chapter 4: Off and Running...With Someone Else's Help

Part 3: JavaScript: Espresso for the Masses

Chapter 5: Objects, Names, and Literals Chapter 6: Operators and Expressions Chapter 7: Making a Statement Chapter 8: Conjuction Junction, What's a Function? Chapter 9: An Eye on Events Chapter 10: How Much Is That Object in the Window? Chapter 11: Mapping and Tracking: Locations and Histories Chapter 12: The Document Shuffle Chapter 13: Fiddling with Forms and Emulating Events Chapter 14: Navigating with Navigator Chapter 15: "Array" (A Ray) of Organization Chapter 16: Image-ine That! Chapter 17: Care for a Cookie?

(1 of 2) [01.06.2001 00:30:57]

Complete Idiot's Guide to JavaScript

Part 4: Scripting the Whole Bean

Chapter 18: Script Example 1: Hello World! Chapter 19: Script Example 2: Random Chatter Chapter 20: Script Example 3: Which Way Do We Go? Chapter 21: Script Example 4: Loan Calculator Chapter 22: Script Example 5: BlackJack Chapter 23: Script Example 6: Creating an eZine Chapter 24: Script Example 7: Welcome Back! Chapter 25: What to Do When It Won't Work: Debugging Your Scripts Chapter 26: Ideas Chapter 27: Stepping Up to Straight Java

Appendix A: JavaScript: The Complete Overview Appendix B: Speak Like a Geek: The Complete Archive

Complete Idiot's Guide to JavaScript

Chapter 1 Coffee? In My Computer? I Prefer Decaf...

In This Chapter

r How HTML got a boost from CGI r Java takes CGI one step farther

Extending the Web

In the "old days" of the World Wide Web (three whole years ago), there were two ways to get information (also called content) to the user. The primary way was through HTML (HyperText Markup Language), the language used to write Web pages. HTML enables you to present text and certain types of graphics (as well as links to connect one page to another page) either on the same computer or somewhere else in the world. As HTML has evolved (the current standard being worked on is version 3.2), other features have been added, such as forms, frames, tables, and so on. However, even with all the new features, HTML basically deals with Web content by:

r Formatting and displaying the content of a page. r Waiting for the user to click something in the page. r Depending on what the user clicks on, fetching something else (a new page, for example) and repeating

the process.

Although this provides a wealth of possibilities for content manipulation (just spend a little time on the Web to see for yourself), it doesn't allow for more advanced things like accessing a database, ordering catalog items online, or making animated graphics within a Web page. For these capabilities, you need to understand the Common Gateway Interface, or CGI.

CGI provides a means of extending the capabilities of HTML by allowing the Web designer to write custom programs that interact with Web pages to do more complex things. A CGI program is a file that resides on the Web server and that the server runs in response to something inside the Web page. With CGI, you can:

r Create image maps, which are graphics that you can click on. Different areas of the graphic behave like unique HTML links, taking you to individual pages. (You used to have to know CGI programming to create one of these.)

An image on the image map

Which part of an image you click determines what happens next.

r Dynamically (on demand) create custom HTML pages and graphics. A common use is the "You are the 34251th visitor" line you find on many pages.

r Create simple animations by having CGI send a sequence of graphics to the browser. This produces the Web equivalent of a "flip-book," where one graphic replaces the previous one.

Pushy Servers The technical term for CGI "flip-book" animation is server push.It's so named because

(1 of 5) [01.06.2001 00:31:16]

Complete Idiot's Guide to JavaScript

the CGI program instructs the server to send (or "push") one graphic after another to the browser. Conversely, you could also use CGI to instruct the browser to "pull" one graphic after another from the server. This technique is (not surprisingly) called client pull.

r Interface with a database on the server to retrieve or add information. Online catalog ordering uses CGI, as well as the earch engines (such as Yahoo, Lycos, and WebCrawler) that keep track of everything there is to find on the Web

One disadvantage of CGI is that you must be a programmer to use it. Secondly, C GI requires that the user's actions be reported back to the server for interpretation and processing. The results of this processing then must be sent back to the user from the server. These extra transfers take time and reduce the "immediacy" of certain Web page interactions. Furthermore, you are limited to the CGI capabilities of your server; your provider might offer a Web server w ith incomplete CGI tools or with none at all. In addition, multimedia (sound, graphics, animation) has become all the rage, and everything in c omputers today has to support multimedia. CGI doesn't do this well.

Most CGI Programs Are Written in Perl

Because the Internet originated within the UNIX world (before Windows computers or Macintoshes were hooked up to it), much of what drives the Internet (and the Web) is based in UNIX. CGI stems from this same root, and the Perl language is a UNIX-based language. However, a CGI program can be written in any language that the Web server supports.

Finally, to use CGI, you must have access to the CGI interface of the Web server that's serving up your pages. As I've mentioned, some providers might not support CGI access, or it might be offered for an extra (in many cases, costly) fee.

In other words, CGI is more complex than most Web authors are interested in, and doesn't support all of the visually fancy things authors want to include in their pages. But something else is necessary, and that something is Java.

Java: Web Programming for the Common Man

You can't surf the Web today without hearing about Java. Java, a programming language developed by Sun Microsystems, was designed to allow more power and flexibility for the presentation of material on the Web. With Java, you can...

r Create animations that sing and dance. r Include prompts and dialog boxes that pop up while a user is filling out a form. r Develop games and programs that actually run--right on the Web page. r Calculate a loan in real-time based on user input. r Display an accurate on-screen invoice reflecting a user's current purchases. r Access databases and other information sources.

(2 of 5) [01.06.2001 00:31:16]

Complete Idiot's Guide to JavaScript

r Let your imagination wander.

Java works the floor in 3D rotating glory.

Before Java, if you wanted to view a graphic, play a movie, or listen to a sound file on the Web, you had to have a helper application (an independent program unrelated to the browser) installed on your computer and connected to your browser. Whenever a particular file (movie, sound, or whatever) was retrieved from the Web, the helper would be run to display or play back the file. If you didn't have the necessary helper, you had to find it, download it, and install it.

Java handles these things internally. No more helper applications, and no more CGI programming. All you need is a Java-enabled browser to view Java programs, and the Java Developers Kit to design them (it's available for free from Sun's Java Home Site, ). And, as an added bonus, the Java programs you create (called applets or mini-applications) will run on any Java-enabled browser on any platform: Macintosh, Windows, or UNIX. You don't need to create a program for each machine type. One size fits all.

However, Java is not without its problems. It also is a programming language, and as with all programming languages, you must learn it relatively well in order to use it. The applets you create must be compiled (by a compiler) before you can use them. A compiler is a special program that reads your own program and crunches it into machine-readable binary code. In spite of the existence of several nice development packages for building Java applets, compilers can be a hassle because you have to use them every time you make a change to your program, and it can take a long time to compile a program.

Scrolling ticker tape Scrolling ticker tapes--trust me; they scroll.

So what you need is a way of getting the capability of Java without the added baggage of a full-blown programming language. Well, this time, Netscape comes to the rescue (with a little help from Sun) with JavaScript.

Enter JavaScript

JavaScript is a scripting language specifically designed to work with the World Wide Web. With JavaScript, you can take the power of HTML and the World Wide Web and extend it in a variety of ways.

Once upon a time (not too long ago), Sun Microsystems conjured up the complex and powerful programming language now known as Java. Although Java is highly capable, it's best suited for more complex tasks and experienced programmers. Netscape Communications saw the need for an in-between language--one that would allow individuals to design Web pages that could interact with the user or with Java applets, but that would require a minimum of programming experience. Always one to be first on the block, Netscape whipped up LiveScript.

LiveScript was more of a proposal than an actual language, but it convinced people that this "bridge-the-gap" idea was attractive. LiveScript was designed in the spirit of many simple scripting languages, but tailored with capabilities specifically designed for Web pages (HTML and form interaction, for example). To help the marketability of their new language, Netscape paired up with Sun, and JavaScript was born.

In theory, JavaScript is available for free to those who design Web browsers; this enables the designers to incorporate JavaScript compatibility into their browsers. In reality, Microsoft was the first to implement a version

(3 of 5) [01.06.2001 00:31:16]

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download