Notes for document. Write () and writeln () Methods

Source: Internet
Author: User

One of the most common methods for BOM document objects is write () or its sibling method writeln (). Both methods accept a parameter, that is, the string to be written to the document. As expected, the only difference between them is that the writeln () method will add a linefeed (n) at the end of the string ).
Both methods Insert the string content to the location where they are called. In this way, the browser will process the document string like the regular HTML code on the page. Consider the following page:

<HTML>
<Head>
<Tltle> document write example </title>
</Head>
<Body>
<H1> <SCRIPT type = "text/JavaScript"> document. Write ("this is a test") </SCRIPT> </Body>
</Html>

The page looks the same in the browser as the following page:
<HTML>
<Head>
<Title> document write example </title>
</Head>
<Body>
<H1> This is a test </Body>
</Html>
You can use this function to dynamically introduce external JavaScript files. For example:

<HTML>
<Head>
<Title> document example </title>
<SCRIPT type = "text/JavaScript">
Document. Write ("<SCRIPT type = \" text/JavaScript \ "src = \" External. js \ ">" + "</src" + "ERT ");
</SCRIPT>
</Head>
<Body>
</Body>
</Html>

This code writes a <script/> tag on the page, which enables the browser to load external JavaScript files as usual. Note that the string "</SCRIPT>" is divided into two parts ("</src" and "IPT> ").

This is necessary because every time a browser encounters </SCRIPT>, it assumes that the code block is complete (even if it appears in a javascript string ). Assume that the preceding example does not divide "</SCRIPT>" into two parts:

<HTML>
<Head>
<Title> document example </title>
<SCRIPT type = "text/JavaScript">
Docunment. write ("<SCRIPT type = \" text/JavaScript \ "src = \" external. JS \ ">" + "</SCRIPT>"); // this will cause a problem
</Head>
<Body>
</Body>
</Html>

The browser displays the following webpage:
<HTML>
<Head>
<Title> document example </title>
<SCRIPT type = "text/JavaScript">
Document. Write ("<SCRIPT type = \" text/JavaScript \ "src = \" External. js \ ">"
</SCRIPT>
</SCRIPT>
</Head>
<Body>
</Body>
</Html>

As you can see, we forget to divide the string "</SCRIPT>" into two parts, causing serious confusion. First, there is a syntax error in the <script/> label, because the brackets are missing when calling document write. Second, there are two </SCRIPT> labels. This is why the "</SCRIPT>" string must be separated when the <script/) label is written to the page using the document. Write () method.

Remember, to insert content attributes, you must call the write () and writeln () methods before fully loading the page. If any method is called after the page is loaded, it will erase the page content and display the specified content.

The open () and close () methods are closely related to the write () and witeln () methods. The open () method is used to open the loaded document for writing. The close () method is used to close the document opened by the open () method, essentially telling it to display all the content written into it. These methods are usually combined to write content to the framework or a new window, as shown below:

VaR onewwin = Window. Open ("about: blank", "newwindow", "Height = 150, width = 300, Top = 10, Left = 10, resizable = yes ");

Onewwin.doc ument. open ();
Onewwin.doc ument. Write ("<HTML> Onewwin.doc ument. Write ("<body> This is a new window! </Body> Onewwin.doc ument. Close ();

In this example, open a blank page (using the local "about: blank" url) and write it to a new page. To implement this operation correctly, call the open () method before calling write. After writing, call the close () method to complete the display. This method is useful when you want to display pages that do not need to return to the server.

Notes for document. Write () and writeln () Methods

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.