A Gentle Introduction to JavaScript

Welcome to my new Macworld.com JavaScript column! I'll be writing an introductory-level monthly piece, with the goal of teaching JavaScript to those of you who've worked with HTML before and are now thinking about taking that next step forward. To start, I'll cover what JavaScript is (and isn't).

JavaScript is what's known as a scripting language, which means that it is (in general) easier to learn than a full-blown programming language such as C++ or Java. You don't need any special tools, programs, or compilers to write JavaScript; whatever you're currently using to write HTML should work just fine. Personally, I prefer to use Bare Bones Software's BBEdit, but WYSIWYG tools such as Adobe GoLive and Macromedia Dreamweaver will do the trick, too. Some people are happy coding their pages with SimpleText, but that's a little too minimalist for my taste. If you don't have any of these (except SimpleText, of course), check out the freeware BBEdit Lite.

JavaScript can be used to make your Web pages interactive and dynamic. A static HTML page without any JavaScript just sits there -- if a visitor returns to your site next week, it'll look exactly the same as it did today. With JavaScript, you can display different images, give feedback on forms, control the user's browser (for instance, displaying different pages based on the user's plug-ins), and manage framed sites. Overall, you use JavaScript to give the user feedback: the feeling that your site is responsive to their actions.

The most common use of JavaScript is the ubiquitous image rollover. It's gotten to the point on the Web where if you don't use JavaScript to change your clickable buttons, some users won't click on them, as they won't realize that they have that option. Image rollovers have become a de facto Web user interface standard, so you should use them if you want to give your visitors the experience they expect.

Other feats you can accomplish with images include slide shows, displaying random graphics, and making images move around the browser window. With JavaScript and forms, you can validate that the information has been entered correctly, cross-check fields against each other, and use one field to set another. With JavaScript and frames, you can force your site either into or out of a frame set, as well as to use information in one frame to change the display of another. You can also change your page based on the date, time, browser, available plug-ins, and/or platform of your site's visitors.

JavaScript is sometimes referred to as JScript or ECMAScript. These aren't exactly the same languages as JavaScript, but the name JavaScript is often used to include them, too.

There are a number of common misconceptions about JavaScript. I'll just clear them up now.

JavaScript Isn't Java At one time, Netscape added a little scripting language to Navigator 2, which they called LiveScript. They had high hopes for this language, but hardly anyone paid attention to it -- most of the mindshare was being given to a new language called Java, which had been recently released by Sun Microsystems. At that point, if you believed some of the press reports, Java was going to cure cancer, feed the hungry, and bring about world peace. How could a new little scripting language compete against that?

The marketing department at Netscape decided that it was better to switch than fight, and (with permission from Sun) changed the name of LiveScript to JavaScript. After that, getting attention became much easier. Unfortunately, this led to the common side effect that many people think that they're the same language, or that JavaScript is a "light" version of Java, or that Sun invented JavaScript. None of these stories are true, and the only thing that the two have in common are the first four letters.

JavaScript Isn't a Security Risk This myth started due to a number of horror stories that tried to convince everyone that they should turn JavaScript off because otherwise, bad, bad things could happen to you. The stories just aren't true. There are no verified reports of anyone, ever, using JavaScript by itself in a way that creates a security hole on a live site. And in every case where someone has written an exploit using JavaScript combined with a server-side program, the hack could have been done just as well without JavaScript. So go ahead and turn JavaScript on; it won't bite.

JavaScript Can't Do Everything While I wish that JavaScript could do everything, it can't. JavaScript can't read from or write to your hard drive, and JavaScript can't send e-mail. JavaScript also can't get rid of the need for server-side programs; for instance, you can't use it to keep track of visit counters or store form info in a database.

With this information under your belt, you'll be ready to start coding by the time the next column appears, which will be in about a month.

DORI SMITH is co-author of the best-selling JavaScript for the WWW: Visual QuickStart Guide, 3rd Edition and author of Java for the WWW: Visual QuickStart Guide. In addition to writing, she has been programming for more than twenty years and is a frequent speaker at industry conferences.

1 2 Page 1
Shop Tech Products at Amazon