Differences between document. write and document. writeln in js,

Source: Internet
Author: User

Differences between document. write and document. writeln in js,

Both of them are methods of JavaScript output to the client. We can see that the difference in writing is short for ln-line. In other words, the writeln method outputs data in rows, which is equivalent? Winte? Add a line break to the output

Note: document. the write method can be used in two aspects: Creating webpage content using real-time scripts during webpage loading and creating content in this window or new window using delayed scripts. this method requires a string parameter, which is the HTML content written to a window or frame. this string parameter can be a variable or expression with a string value. The written content often contains HTML tags.

Remember, after loading the webpage, the browser output stream will be automatically closed. after that, any document for the current webpage. the write () method will open a new output stream, which will clear the output content of the current webpage (including any changes in the source document ). therefore, if you want to replace the current webpage with the HTML content generated by the script, you must connect the HTML content and assign it to a variable. here, document. write () to complete the write operation. you do not have to clear the document and open a new data stream, a document. the write () call is OK.

About document. the write () method also needs to describe its related method document. close (). the output stream must be closed after the script is written to the window (whether this window or other windows. in the last document of the script. after the write () method. make sure there is document. close () method. in this case, the image and form cannot be displayed. in addition, any document. write () will only append the content to the web page, instead of clearing the existing content, write a new value

Procedure:

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

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

Document. write ("Line1 ")
Document. write ("Line1 ")

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

Document. writeln ("Line1 ")
Document. writeln ("Line2 ")

4. complete code example:

<script> with(window.open()){ document.write("Line1") document.write("Line1") document.writeln("Line1") document.writeln("Line2") } </script>

Note: The two methods are different only when you view the source code.
Note: add the above Code to the webpage and view the source code in the pop-up window. You will see:

Line1Line1Line1
Line2

Page effects and source code.

Note

In general, there is no difference in the effect of the two output methods on the page (unless it is output to the pre or xmp element ).

Ii. document. write () write html to a specified location

The page can be correctly written in the select box during page Initialization
But it was written outside the control during the call. I wonder if document. write () Can I change innerHTML or outerHTML to dynamically write HTML? And the HTML to be used to display what to do?

As follows:

<Html> 

Test the retention format:

<Script> document. write ("<pre> I won't wrap in pre! ") Document. write (" I won't wrap in pre! ") Document. writeln (" I will wrap in pre! ") Document. writeln (" I will wrap in pre! ") Document. writeln (" I will wrap in pre! </Pre> ") </script>

Difference between Write and Writeln

Write cannot wrap, and Writeln can wrap.

How to view the newline Effect of Writeln

The newline Effect of writeln cannot be seen in the webpage. It is displayed as a space by the browser.
The effect is not visible in HTML files and JSP source files. You can add a pre-format tag to the tag to view the effect.

Helper's house editor added: You can view it in chrome through f12

<script>   document.write("<pre>write");   document.writeln("writln");   document.write("write</pre>");</script>

In addition to the above, readers can use the open method to re-open a window to view

<script> with(window.open()){ document.write("write") document.writeln("writeln") document.writeln("write") }</script>

Then, you can view the webpage source file in the pop-up window to see the effect. I tested that the source file is not viewed in the pop-up window in chrome 56.0.2924.3. In this case, you can "check" and then see the effect in the Element column, both IE11 and Firefox50.0 have the View Source File column.

Add:

<Html> 

View through chrome F12

Note:

Note: document. writeln (like document. write) does not work in XHTML documents ents

Write and writeln do not work in XHTML files. HTML is the XHTML with relatively loose syntax, which explains why there is no line break in html. Click "View.

Html, xhtml, and xml definitions:

1. html is the Hyper Text Markup Language (Hyper Text Markup Language), which was the first Language to write web pages. However, due to the early time, the standards are not very good. It is case-insensitive and the encoding is not standard;
2. xhtml is the Extensible Hyper Text Markup Language. It standardizes html and provides more rigorous and pure encoding. It is also a Language for the transition from html to xml;
3. xml is an Extensible Markup Language (Extensible Markup Language). It is a cross-platform Language that allows you to create tags as needed.
4. webpage encoding evolved from html> xhtml> xml.

Differences between html, xhtml, and xml:

1. Comparison between xhtml and html. The xhtml document has a complete typographical structure, which is embodied in two aspects: a. elements must have end tags; B. elements must be nested;
2. For html elements and attributes, xhtml must be in lowercase, Because xml is case sensitive and <li> and <LI> are different labels;
3. The xhtml attribute values must be enclosed in quotation marks;
4. xhtml does not support attribute minimization. What is attribute minimization?
Correct: unminimized attributes)
<Input checked = "checked">
Incorrect: minimized attributes)
<Input checked>
5. In xhtml, the name attribute is not in favor of use and will be deleted in future versions.

Besides, why does webpage coding develop from html> xhtml> xml?

In other words, early web pages are written in html, but they have three major drawbacks:

1. The encoding is not standard and the structure is messy and bloated. intelligent terminals are required to display the code properly;
2. Chaotic performance and structure are not conducive to development and maintenance;
3. You cannot use more network devices, such as mobile phones and PDAs;
Therefore, HTML needs to be developed to solve this problem, so W3C has developed XHTML, which is a bridge between HTML and XML. Xml is the development trend of web.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.