Home > Web Front-end > JS Tutorial > Explanation of the difference between the output content of document.write and writeln_Basic knowledge

Explanation of the difference between the output content of document.write and writeln_Basic knowledge

WBOY
Release: 2016-05-16 18:18:04
Original
1165 people have browsed it

document.write() //Write the content into the document, the current editing position is the last character of the written content
document.writeln() //Write the content into the document, and add a newline character, the current editing position The next line of the written content

document.write() and document.writeln are both JavaScript methods for writing to the client. writeln is output in line form, but it does not refer to the actual effect of the page. Line breaks, the difference between the two methods can only be seen when viewing the source code, unless it is output to a pre or xmp element

Test it:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Run the above code, in In the newly opened window: View - Source File, you can see that writeln outputs

in line format. The difference in the actual effect is that the line break in the source code will cause a "space" in the actual effect. Compare the following examples to understand:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

The input result is actually: 111 222 So in the application, using write will be more convenient than writeln, and will not produce unnecessary space effects, so my Html and Js online mutual conversion gadget [
http://www .jb51.net/tools/html-js.htm
] I specially changed the way most people use writeln on the Internet to write. I believe it will bring convenience to everyone.
Regarding retaining the format, test it:
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

The difference between document.write() and document.writeln() Use js to write dynamic select


Solution:

Both are methods of JavaScript output to the client. Comparison shows The difference in writing is the abbreviation of ln--line. In other words, the writeln method outputs in lines, which is equivalent to adding a newline character after the ?winte? output.

Note: The document.write method can be used in two ways: using real-time scripts to create web page content during web page loading and using delayed scripts to create the content of this window or a new window. This method requires a string Parameter, which is the HTML content written to the window or frame. The string parameter can be a variable or an expression whose value is a string. The written content often contains HTML tags.

Remember, load the web page After that, the browser output stream will be automatically closed. After that, any document.write() method on the current web page will open a new output stream, which will clear the output content of the current web page (including any changes in the source document). and value). Therefore, if you want to replace the current web page with the HTML content generated by the script, you must concatenate the HTML content and assign it to a variable. Here, use document.write() to complete the writing operation. There is no need to clear the document and open a new one data flow, a document.write() call is OK.

Regarding the document.write() method, you also need to explain its related method document.close(). The script writes to the window (whether it is this window Or other windows) The output stream must be closed after writing the content. After the last document.write() method of the script. You must ensure that there is a document.close() method. If you do not do this, images and forms cannot be displayed. Moreover, later calls Any document.write() will only append the content to the web page, but will not clear the existing content and write new values ​​

Specific steps:

1. Open a blank window.
window.open()

2. Use the write method to write code to the blank window.

document.write("Line1")
document.write("Line1")

3. Use the writeln method to write code to the blank window.

document.writeln("Line1")
document.writeln("Line2")
4. Complete code example: Copy code
<script> with(window.open()){ document.write("百度") document.write("百度") document.writeln("知道") document.writeln("知道") document.writeln("知道") } </script><script> document.writeln("111") document.writeln("222") </script> The code is as follows:<script> document.write("<pre class="brush:php;toolbar:false">百度&quot;) document.write(&quot;百度&quot;) document.writeln(&quot;知道&quot;) document.writeln(&quot;知道&quot;) document.writeln(&quot;知道&quot;) &lt;/script&gt;&lt;div class=&quot;codebody&quot; id=&quot;code97054&quot;&gt; &lt;br&gt;&lt;script&gt; &lt;br&gt;with(window.open()){ &lt;br&gt;document.write(&quot;Line1&quot;) &lt;br&gt;document.write(&quot;Line1&quot;) &lt;br&gt;document.writeln (&quot;Line1&quot;) &lt;br&gt;document.writeln(&quot;Line2&quot;) &lt;br&gt;} &lt;br&gt;&lt;/script&gt; &lt;br&gt; &lt;/div&gt; &lt;br&gt;&lt;br&gt;Note: Both methods are only available when viewing the source You can only see the difference when looking at the code. &lt;br&gt;Special tip: Add the above code to the web page, and then view the source code of the pop-up window, you will see: &lt;br&gt;&lt;br&gt;Line1Line1Line1 &lt;br&gt;Line2 &lt;br&gt;&lt;br&gt;The page effect and source code are as follows picture. &lt;br&gt;&lt;br&gt;&lt;img src=&quot;http://files.jb51.net/file_images/article/201402/20140221170111.jpg&quot; alt=&quot;Explanation of the difference between the output content of document.write and writeln_Basic knowledge&quot; &gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;Special Note&lt;br&gt;&lt;br&gt;In general, there is generally no difference in the output effects of the two methods on the page (unless It is output to the pre or xmp element). &lt;br&gt;&lt;br&gt;&lt;strong&gt;2. document.write() writes html to the specified location &lt;br&gt;&lt;/strong&gt;&lt;br&gt;When the page is initialized, it can be written correctly in the select box &lt;br&gt;But when it is called, it is written in the control Besides, I wonder if document.write() can change innerHTML or outerHTML to dynamically write HTML? And how to deal with the written HTML for display? &lt;br&gt;&lt;br&gt; is as follows: &lt;br&gt;&lt;br&gt;&lt;div class=&quot;codetitle&quot;&gt; &lt;span&gt;&lt;a style=&quot;CURSOR: pointer&quot; data=&quot;78491&quot; class=&quot;copybut&quot; id=&quot;copybut78491&quot; onclick=&quot;doCopy('code78491')&quot;&gt;&lt;u&gt;Copy code &lt;/u&gt;&lt;/a&gt;&lt;/span&gt; The code is as follows: &lt;/div&gt; &lt;div class=&quot;codebody&quot; id=&quot;code78491&quot;&gt; &lt;br&gt;&amp;lt ;html&gt; &lt;br&gt;&lt;head&gt;&lt;/head&gt; &lt;br&gt;&lt;script type=&quot;text/javascript&quot;&gt; &lt;br&gt;function creatOption(){ &lt;br&gt;for(i=0;i&lt;5 ;i ) &lt;BR&gt;document.write(&quot;&lt;option value='&quot; i &quot;'&gt;&quot; i &quot;&lt;/option&gt;&quot;); &lt;br&gt;} &lt;br&gt;function openWrite(){ &lt;br&gt;var win=window.open(); &lt;br&gt;win.document.write(&quot;Line1&quot;); &lt;br&gt;win.document.write(&quot;Line1&quot;); &lt;br&gt;win.document.write(&quot;&lt;input type ='text' value='1234567890' /&gt;&quot;); &lt;br&gt;win.document.writeln(&quot;Line1&quot;); &lt;br&gt;win.document.writeln(&quot;Line2&quot;); &lt;br&gt;} &lt;br&gt; &lt;/script&gt; &lt;br&gt;&lt;body&gt; &lt;br&gt;&lt;select id=&quot;myselect&quot; name=&quot;myselect&quot;&gt; &lt;br&gt;&lt;script language=&quot;javascript&quot;&gt; &lt;br&gt;creatOption(); &lt;br&gt;&lt;/script&gt; &lt;br&gt;&lt;/select&gt; &lt;br&gt;&lt;input type=&quot;button&quot; value=&quot;Button&quot; onclick=&quot;openWrite()&quot;/&gt; &lt;br&gt;&lt;/body&gt; &lt;br&gt;&lt;/html&gt; &lt;br&gt; &lt;/div&gt; &lt;br&gt;&lt;br&gt; Regarding retaining the format, test it: &lt;script&gt; document.write(&quot;&lt;pre class=&quot;brush:php;toolbar:false&quot;&gt;I don’t wrap lines in pre!&quot;) document.write(&quot;I will not wrap lines in pre!&quot;)document.writeln(&quot;I will wrap lines in pre!&quot;)document.writeln(&quot;I will wrap lines in pre!&quot;)document.writeln(&quot;I will wrap lines in pre Line breaks will occur in pre!</pre>") </script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template