The difference between JavaScript document.write and document.writeln

 Document.write and document.writeln are both the syntax used by JavaScript to output strings. In fact, there is almost no difference between them. So why are they divided into write and writeln? Take a closer look at writeln that has more ln than write, that is, it means more line. The difference is that writeln is output in the form of " lines ". That's it. Assuming that there is a coherent string, it does not need to be divided into multiple lines. For direct output, use document.write . If you want to output multiple lines, use document.writeln .


Basic syntax of document.write and document.writeln

document.write("string to be output");   document.writeln("string to be output");


The usage of the two is almost the same, it seems that there is no difference, in fact, the effect can only be shown when the code is written.

document.write and document.writeln examples
<pre>
<script language="javascript">
document.writeln("Welcome to Wibibi.");
document.writeln("Have a good time.");

document.write('Welcome to Wibibi.');
document.write('Have a good time.');
</script>
</pre>
The output of the above example
Welcome to Wibibi.
Have a good time.
Welcome to Wibibi.Have a good time.
In order to compare the differences between document.write and document.writeln , document.writeln must be wrapped with <pre></pre> tags to be effective, so the example uses <pre></pre> tags to Wrap up the JavaScript Code, you can see that the two outputs of writeln are divided into different lines, while the two outputs of write are not divided into two lines. Such a simple string output writing method, even if you pull document.write to <pre ></pre> Outside the tag, it will not be divided into two lines.

In fact, document.write can also output newline effects! However, you need to add some HTML line break tags, such as <br> or <p> tags, which can also achieve the effect of line breaks, and you don’t need to use <pre></pre> tag packaging. Although it is a bit troublesome, it can also be made with The same effect as document.writeln , free choice!

Post a Comment

0 Comments