HTML and CSS Reference
In-Depth Information
The results of a
simple script.
The page includes a single line of JavaScript, which writes the “Printed by JavaScript”
on the page. First of all, that text is printed between the other two paragraphs on the
page, demonstrating that the browser executed the JavaScript as soon as it got to it. As
you can see, document.write() adds text to the page source. What this code does is call
the write method of the document object, which takes a single parameter—the text to be
added to the page. For now, it's important to know that the document object is a repre-
sentation of the current page that is accessible by JavaScript. A method represents a type
of message that can be sent to an object, the write() method tells the document object
to add some text to the page. I talk a lot more about the other objects that the browser
provides to JavaScript a bit later. Before that, I discuss the argument that I passed to
document.write() a bit more.
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. When you pass a value to a method,
it's called an argument . So you say that document.write() expects a string argument,
which it then adds to the source of the document. JavaScript is a loosely typed language,
so even though document.write() expects a string, you don't actually have to give a
string to work with. You can give it any type of data you want, and it will do its best to
turn it into a string and print it on the page. So, for example, you can give it a number:
<script type=”text/javascript”>
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 type=”text/javascript” charset=”utf-8”>
The results are in Figure 14.2.
Search WWH ::

Custom Search