Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

The JavaScript Language 477

17




Printed after JavaScript is run.




Output ▼


The page includes a single line of JavaScript, which writes the “Printed by JavaScript” on
the page. The text is printed between the other two paragraphs on the page, demonstrat-
ing that the browser executed the JavaScript as soon as it got to it.


Before I start talking about what this code does, let me talk for just a moment about its
structure. JavaScript programs are made up of individual statements, which are termi-
nated by a semicolon. This program consists of a single statement or line of code.


As you can see, this line adds text to the page. Let me break down what this does,
exactly. First, you might notice that document.write() and window.alert(), which I
use earlier, look similar. Both window and document are things. write and alert are mes-
sages that you can send to those things. In both cases, you need content for those mes-
sages. In the latter example, the content of the write message is “Printed by JavaScript.”


In programming, the term used for things is objects, and the term used for the messages
is methods. The content of the message is said to be passed to the method and is referred
to as the method’s argument. So in programmer jargon, I’d say that I called the write
method of the document object with the argument “Printed by JavaScript.” The document
and window objects are provided by the browser—the environment in which JavaScript
runs. You can also declare objects of your own; they’ll be discussed further along.


FIGURE 17.3
The results of a
simple script.

Free download pdf