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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

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...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

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.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles