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:
]
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:
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.
[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">百度")
document.write("百度")
document.writeln("知道")
document.writeln("知道")
document.writeln("知道")
</script><div class="codebody" id="code97054"> <br><script> <br>with(window.open()){ <br>document.write("Line1") <br>document.write("Line1") <br>document.writeln ("Line1") <br>document.writeln("Line2") <br>} <br></script> <br>
</div> <br><br>Note: Both methods are only available when viewing the source You can only see the difference when looking at the code. <br>Special tip: Add the above code to the web page, and then view the source code of the pop-up window, you will see: <br><br>Line1Line1Line1 <br>Line2 <br><br>The page effect and source code are as follows picture. <br><br><img src="http://files.jb51.net/file_images/article/201402/20140221170111.jpg" alt="Explanation of the difference between the output content of document.write and writeln_Basic knowledge" ><br><br><br>Special Note<br><br>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). <br><br><strong>2. document.write() writes html to the specified location <br></strong><br>When the page is initialized, it can be written correctly in the select box <br>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? <br><br> is as follows: <br><br><div class="codetitle">
<span><a style="CURSOR: pointer" data="78491" class="copybut" id="copybut78491" onclick="doCopy('code78491')"><u>Copy code </u></a></span> The code is as follows: </div>
<div class="codebody" id="code78491"> <br>&lt ;html> <br><head></head> <br><script type="text/javascript"> <br>function creatOption(){ <br>for(i=0;i<5 ;i ) <BR>document.write("<option value='" i "'>" i "</option>"); <br>} <br>function openWrite(){ <br>var win=window.open(); <br>win.document.write("Line1"); <br>win.document.write("Line1"); <br>win.document.write("<input type ='text' value='1234567890' />"); <br>win.document.writeln("Line1"); <br>win.document.writeln("Line2"); <br>} <br> </script> <br><body> <br><select id="myselect" name="myselect"> <br><script language="javascript"> <br>creatOption(); <br></script> <br></select> <br><input type="button" value="Button" onclick="openWrite()"/> <br></body> <br></html> <br>
</div> <br><br> Regarding retaining the format, test it: <script> document.write("<pre class="brush:php;toolbar:false">I don’t wrap lines in pre!") document.write("I will not wrap lines in pre!")document.writeln("I will wrap lines in pre!")document.writeln("I will wrap lines in pre!")document.writeln("I will wrap lines in pre Line breaks will occur in pre!</pre>") </script>