Newlines are not <BR> tags (Pitfall)

A newline in a script does not display a line break in HTML output.

Because HTML is fairly freely formatted, you have to explicitly tell it when a line break is to appear. You can do this a variety of block level tags. Its most likely done with a <BR> or <P> tag.

When you use the document.write() method, you need to explicitly include the necessary <BR> or <P> tags otherwise the HTML output that gets displayed will all run onto a single line. Placing a \n newline escape into the output may make the HTML source look nice but it won't affect the displayed output.

A line break is also introduced when you use block structured elements in the HTML. Here is a list of common HTML tags that do this (there are others too):

See also:Pitfalls


Wrox Instant JavaScript - page - 46