HTML and CSS Reference
In-Depth Information
EXAMPLE
2.2 (
CONTINUED
)
document.writeln("Three, ");
3
document.write("Blast off....<br />"); // break tag
4
//document.write("The browser you are using is " +
//
navigator.userAgent + "<br />");
5 </script>
6
<pre>
7 <script type="text/javaScript">
/*Lines are broken due to size of this page. If you cut
and paste these programs into an editor, make sure
strings start and end with qutoes!!!*/
8
document.writeln
("With the <em>HTML <pre>
</em> tags, ");
document.writeln("the <em>writeln</em> method produces a
newline.");
document.writeln("Slam");
document.writeln("Bang");
document.writeln("Dunk!");
9 </script>
10
</pre>
</big>
</body>
</html>
EXPLANATION
1
The
document.write()
method does not produce a newline at the end of the string
it displays. HTML tags are sent to the HTML renderer as the lines are parsed.
2
The
document.writeln()
method doesn't produce a newline either, unless it is in an
HTML
<pre>
tag.
3
Again, the
document.write()
method does not produce a newline at the end of the
string. The
<br>
tag is added to produce the line break.
4
The
document.write()
method does not produce a newline. The
<br />
tag takes
care of that.
userAgent
is a special
navigator
property that tells you about your
browser.
5
The first JavaScript program ends here.
6
The HTML
<pre>
tag starts a block of preformatted text; that is, text that ignores
formatting instructions and fonts.
7
This tag starts the JavaScript code.
8
When enclosed in a
<pre>
tag, the
writeln()
method will break each line it prints
with a newline; otherwise, it behaves like the
write()
method (i.e., you will have
to add a
<br>
tag to get a newline).
9
This tag marks the end of the JavaScript code.
10
This tag marks the end of preformatted text. The output is shown in Figure 2.3.