04.07.2013 Views

Start Here! Learn JavaScript - Cdn.oreilly.com

Start Here! Learn JavaScript - Cdn.oreilly.com

Start Here! Learn JavaScript - Cdn.oreilly.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

A First <strong>JavaScript</strong> Program<br />

Later in this chapter, you’ll see how to create your own program in <strong>JavaScript</strong>, but in the interest of<br />

getting you thinking about code right away, here’s a small webpage with an embedded <strong>JavaScript</strong><br />

program:<br />

<br />

<br />

<br />

<strong>Start</strong> <strong>Here</strong><br />

<br />

<br />

<br />

document.write("<strong>Start</strong> <strong>Here</strong>!");<br />

<br />

<br />

<br />

You’ll see later how to create a page like the one shown here. When viewed in a browser, the page<br />

looks like Figure 1-1. I’ll show you how to create such a page later in the chapter.<br />

FIGURE 1-1 A basic <strong>JavaScript</strong> program to display content.<br />

The bulk of the code shown in the preceding listing is standard HTML (HyperText Markup Language)<br />

and will be explained later. For now, you can safely ignore the code on the page except for the<br />

three lines beginning with .<br />

The opening and closing script tags tell the web browser that the up<strong>com</strong>ing text is in the form of<br />

a script—in this case, it is of the type text/javascript. The browser sees that opening script tag and<br />

hands off processing to its internal <strong>JavaScript</strong> interpreter, which then executes the <strong>JavaScript</strong>. In this<br />

case, the entire code is merely contained on a single line: a call to the write method of the document<br />

object, which then places some HTML into the document.<br />

The actual <strong>JavaScript</strong> <strong>com</strong>prises a single line:<br />

2 <strong>Start</strong> here! <strong>Learn</strong> <strong>JavaScript</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!