Online Book Reader

Home Category

Getting Good with JavaScript - Andrew Burgess [1]

By Root 237 0
world. It's used not only in web browsers, but also desktop apps, mobile phone apps, and now even on the server. You definitely won't be wasting your time learning JavaScript.

ROCKSTAR TIP

It's important to note that JavaScript IS NOT Java. There are really only two things that JavaScript and Java have in common. First and most obvious is the word "Java" (which was used to make JavaScript attractive to Java developers; I know, really). The only other things is a bit of similar syntax: both languages use C-style curly braces. That's really all.

How Do I Get Started?


Now that you're pumped to learn JavaScript, how do we get started? Well, you'll learn how to actually code JavaScript in the following chapters. But where is that code going to go?

Although it's hard to find a place where JavaScript won't run, the overwhelming majority of JavaScript—especially the JavaScript you'll be writing for now—will be in web pages, to run in the browser. There are two ways to get JavaScript into your HTML page.

First, you can embed your JavaScript code right inside script tags.


You don't have to understand the code in the tags for now; just know that it works. When your browser encounters a script tag, it will interpret the code that you've written and execute it (if appropriate).

The second way to get JavaScript onto your page is to link to a .js file:


Yes: unfortunately that closing script tag is required, even though you don't put anything inside it. As you might guess, the src attribute is short for "source." This will download the JavaScript file and process it, just like the inline code.

Two more notes about including scripts:

For the most part, you'll want to include your script tags right before your closing body tag. Actually, you can put them anywhere you want, but it's best for performance reasons to put it at the end of the body … or write your code to wait until the page has completed loading before it begins executing. We'll see how to do this later on (and no, you can't jump ahead to "Another Short Rabbit-Trail on Script Loading").

You might see code in which the script tags have this attribute: type="text/javascript". This used to be required, but isn't in HTML5 (or course, you should then be using an HTML5 doctype).

Firebug

As you learn about JavaScript, you'll find the Firefox plugin Firebug invaluable. Go to that link and install it, right after you install the latest version of Firefox.

After installing Firebug, you'll have a little bug icon in the lower right corner of the status bar of your Firefox window; if it's coloured, that means it's enabled for the website you're on; otherwise it will be black and white.

Fig. 1-1. Firebug activated indicator

At least, that's where it's been for a long time. If you're on Firefox 4, however, it's now on the toolbar:

Fig. 1-2. Firebug activated indicator in Firefox 4

To enable Firebug on a website, just click that icon. When you click on this, the Firebug panel will pop up. It'll look something like this:

Fig. 1-3. The Firebug panel

We could write a whole tutorial on Firebug, but the panel you'll find useful in this book is the console panel. You can find it by clicking the console tab (Firebug should open to it by default; it's pictured above).

At the bottom of the panel, you'll see a prompt (»>) where you can type some JavaScript. This is like the command line, where you type in some code, hit return (or enter), and you'll get the result. If you need to type in more than one line of code at a time, press the upward-facing arrow at the end of the line; you'll be able to type in more than one line, and then click the Run button at the bottom of the expanded panel to execute it.

Conventions Used in This Book


One more administrative note before we continue: many of the code examples in the book will look like this:

Example 1.1


alert("Hello, reader"); // Hello, reader


See how there's a header on this code snippet: "Example 1.1"? All code

Return Main Page Previous Page Next Page

®Online Book Reader