HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [132]
You can discover more about finding and fixing errors in Chapter 3 of this minibook.
Writing Your First JavaScript Program
The foundation of any JavaScript program is a standard Web page like the ones featured in the first three minibooks.
To create your first JavaScript program, you need to add JavaScript code to your pages. Figure 1-1 shows the classic first program in any language.
This page has a very simple JavaScript program in it that pops up the phrase “Hello, World!” in a special element called a dialog box. It’s pretty cool.
Here’s an overview of the code:
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
Figure 1-1:
A JavaScript program caused this little dialog box to pop up!
Hello World?
There’s a long tradition in programming languages that your first program in any language should simply say, “Hello, World!” and do nothing else. There’s actually a very good practical reason for this habit. Hello World is the simplest possible program you can write that you can prove works. Hello World programs are used to help you figure out the mechanics of the programming environment — how the program is written, what special steps you have to do to make the code run, and how it works. There’s no point in making a more complicated program until you know you can get code to pop up and say hi.
As you can see, this page contains nothing in the HTML body. You can incorporate JavaScript with XHTML content. For now, though, you can simply place JavaScript code in the head area in a special tag and make it work.
Embedding your JavaScript code
JavaScript code is placed in your Web page via the pair. The -->