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

May 16, 2016 pm 06:18 PM
write

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;/static/imghw/default1.png&quot; data-src=&quot;http://files.jb51.net/file_images/article/201402/20140221170111.jpg&quot; class=&quot;lazy&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>
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts Mar 15, 2025 am 09:19 AM

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

See all articles