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

(singke) #1
ptg16476052

478 LESSON 17: Introducing JavaScript


The document object is a representation of the current page that is accessible by
JavaScript. Whenever you manipulate the page using JavaScript, you do so by calling
methods of the document object.
The bit of text that I passed to the document.write() method in the previous example
is called a string in the vocabulary of programming. The document.write() method
expects a string argument, which it then adds to the source of the document. Some pro-
gramming languages are strict about data types, so if a method expects you to give it a
string as an argument and you give it a number instead, an error will occur. JavaScript
isn’t like that. If you pass a string to the document.write() method, it will print it out
unchanged. If you pass it some other kind of thing, it will do its best to convert it to a
string and print it out.
So, for example , you can give it a number:
<script>
document.write(500);
</script>

It will convert the number to a string and print it on the page. Or you can even pass it an
object, like this:
<script>
document.write(document);
</script>

Th e results are in Figure 17.4.

Output ▼


FIGURE 17.4
Attempting to write
an object to the
page.
Free download pdf