Multiple Write Statements

Multiple write statements will run together when written to the page even though the text is written on several lines. The reason is that the document.write statement just writes text to the page. It does not add any breaks when its line stop. There are two ways to do line breaks:

  1. Use document.writeln to break the line at the end of the text (can be buggy)
  2. Use an HTML <br /> flag (more trustworthy)
HTML file: Displayed by browser:
<BODY>

<SCRIPT LANGUAGE="javascript">
document.write("This is the text that will be written to the page. ");
document.write("But even though these lines of text are on different lines ");
document.write("in this script, they will not reproduce the same way on the ");
document.writeln("HTML document.");
document.write("This should be a new line created by the buggy writeln.");
document.write("<BR /><BR />Here is another new line created with an HTML <BR />");
document.write("flag and is the better way <BR />");
document.write("to put line breaks onto a document.<BR />");
</SCRIPT>

</BODY>

Good habit Note that there is supposed to be a line break between "HTML document" and "This is a new line created by the buggy writeln". Apparently, document.writeln will write a line to the document, but it does not creat a "line" in the HTML. So it's good JavaScript form to use the HTML <br /> flag and create my own line breaks.
Back Table of Contents Next