HTML and CSS Reference
In-Depth Information
"hot" + "dog" or "San Francisco" + "<br />"
For more information on strings, see Chapter 3, “The Building Blocks: Data Types,
Literals, and Variables.”
2.3.2 The write() and writeln() Methods
One of the most important features of client-side JavaScript is its ability to generate
pages dynamically. Data, text, and HTML itself can be written to the browser on the fly.
The write() method is a special kind of built-in JavaScript function used to output
HTML to the document as it is being parsed. When generating output with write() and
writeln() , put the text in the body of the document (rather than in the header) at the
place where you want the text to appear when the page is loaded.
Method names are followed by a set of parentheses. They are used to hold the argu-
ments. These are messages that will be sent to the methods, such as a string of text, the
output of a function, or the results of a calculation. Without arguments, the write() and
writeln() methods would have nothing to write.
JavaScript defines the current document (i.e., the HTML file that contains the script)
as a document object. (You will learn more about objects later.) For now, whenever you
refer to the document object, the object name is appended with a dot and the name of
the method that will manipulate the document object. In the following example the
write( ) method must be prepended with the name of the document object and a period.
The browser will display this text in the document's window. The syntax is
document.write("Hello to you");
The writeln() method is essentially just like the write() method, except when the text
is inserted within HTML <pre> or <xmp> tags, in which case writeln() will insert a new-
line at the end of the string. The HTML <pre> tag is used to enclose preformatted text.
It results in “what you see is what you get.” All spaces and line breaks are rendered lit-
erally, in a monopitch typeface. The <xmp> tag is an obsolete HTML tag that functions
much like the <pre> tag.
EXAMPLE 2.2
<html>
<head><title>Printing Output</title></head>
<body bgcolor="yellow" text="blue">
<big>
<b>Comparing the <em>document.write</em> and
<em>document.writeln</em> methods</b><br />
<script type="text/javascript">
1
document.write("One, "); // No newline
2
document.writeln("Two, ");
 
 
Search WWH ::




Custom Search