Java Reference
In-Depth Information
The Document Object
Each window object contains a document object. This object has properties and methods
that deal with the page that has been loaded into the window. In Chapter 6, we covered the
Document Object Model and the properties and methods used to manipulate items on the
page. The document object contains other methods that are worth looking at.
document.write()
The write() method simply writes a string of text to the page. If a page has already
loaded, it will completely replace the current document:
document.write("Hello, world!");
This would replace the whole document with the string “Hello, world!”. It is possible to
include HTML in the string and this will become part of the DOM tree. For example, the
following piece of code will create an <h1> tag node and a child text node:
document.write("<h1>Hello, world!</h1>");
The document.write() method can also be used within a document inside <script>
tags to inject a string into the markup. This will not overwrite the rest of the HTML on the
page. The following example will place the text "Hello, world!" inside the <h1> tags
and the rest of the page will display as normal:
<h1>
<script>document.write("Hello, world!")</script>
</h1>
The use of document.write() is heavily frowned upon as it can only be realistically
used by mixing JavaScript within an HTML document. For this reason, there should be no
reason for a ninja to use it, although you might come across it in some old (I hope) tutorials
on the Web.
Search WWH ::




Custom Search