Historically, the Document object supported five methods for controlling output to the document: clear(), close(), open(), write(), and writeln(). Throughout the book we have used the document.write() method to output strings to the document. Yet we really haven’t used the others at all. Let’s take a look at their features to understand why.
First, let’s address the difference between document.write(string) and document.writeln (string). Both methods take strings and output the passed string to the active document. The main difference is that the writeln() method adds a newline character (\n) to its output while the write() method does not. However, under (X)HTML, return or newline characters are ignored except within certain situations like the <<pre>> tag, within a <<textarea>>, or when a CSS white-space property is applied so you may never notice the difference. The following code snippet uses a <<pre>> tag to show the difference between the methods:
The result is shown here:
Using document.write() and writeln(), we have gotten used to writing out (X)HTML to documents. As we have seen, it can be somewhat time consuming to output numerous strings, so it is often better to build a string up and then output it at once like so: