Js document. write ()

Source: Internet
Author: User

Js document. write ()

The most basic JavaScript command is document. write. This command simply prints the specified text content to the page. To print text verbatim, add single quotation marks to the printed text string

After a manned page, the browser's output stream is automatically closed. After that, any document. write () method that operates on the current page opens a new output stream. It clears the content of the current page (including any variables or values of the source document. if you want to replace the current page with the HTML generated by the script, you must connect the HTML content to a variable and use a document. the write () method does not need to clear the document and open a new data stream, a document. write () is called to complete all operations.

 

Another note about the document. write () method is its related method document. close (). After the script finishes writing content to the window (whether this window or another window), the output stream must be closed. After the last document. write () method of the delayed script, make sure that the document. close () method is included. If you do not do so, the image and form cannot be displayed. In addition, the document. write () method called later only appends the content to the page and does not clear the existing content to write new values.

 

Document. write Method

 

The most basic JavaScript command is document. write. This command simply prints the specified text content to the page. To print text verbatim, enclose the printed text string with single quotation marks.

 

The Code is as follows:

Document. write ('Hello World! ');

 

 

The above js Code will display "Hello World!" on the page! "

You can use document. write to print variables. The input variable name is enclosed in quotation marks as follows:

The Code is as follows:

Var mytext = "Hello again ";

Document. write (mytext );

 

 

Note: If the variable name is enclosed in quotation marks, the variable name is printed (the variable value is not printed ). You can use the "+" symbol to connect variable values and text strings.

 

The Code is as follows:

Var colour1 = "purple ";

Var colour2 = "pink ";

Document. write ('<p> colour1:' + colour1 + '<br> colour2:' + colour2 + '</p> ');

 

 

The output is as follows:

 

Colour1: purple

Colour2: pink

 

Document. write is also used to load js advertisements.

 

 

Copy the Code as follows:

Document. write ('<scri' + 'pt src = "http://www.jb51.net/ad.js" type = "text/javascript"> </s '+ 'plain> ');

Document. write ("<scri" + "pt src = 'HTTP: // www.jb51.net/ad.js' type = 'text/javascript '> </s" + "comment> ");

Document. write ("<scri" + "pt src =" http://www.jb51.net/ad.js "type =" text/javascript "> </s" + "inline> ");

 

 

Generally, double quotation marks (double quotation marks) are used to connect characters, and double signals (single quotation marks) are used in the characters. Of course, you can also use escape characters, but it will be troublesome to modify them later.

 

Document. write js loaded in this way is asynchronous

The Code is as follows:

<! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd">

<Html lang = "ru">

<Head>

<Title> </title>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

</Head>

<Script type = "text/javascript">

Function load (js ){

Var s = document. createElement ('script ');

S. setAttribute ('type', 'text/javascript ');

S. setAttribute ('src', js );

Var head = document. getElementsByTagName ('head ');

Head [0]. appendChild (s );

 

}

Function write (js ){

Document. write ('<script type = "text/javascript" src = "' + js + '"> </script> ');

}

 

Load ("http://www.jb51.net/js/2011/jquery-1.5.1.min.js ");

// Write ("http://www.jb51.net/js/2011/jquery-1.5.1.min.js ");

 

</Script>

<Script>

Alert ($ );

</Script>

 

Q: If you load data using createElement ("script"), an error will be reported when you call a function. If you use document. write, no error will be reported?

 

Answer:

 

For dynamically created js references, different browsers have different responses.

For FF Opera, the load method is blocked, so alert ($) can be output, but it is not blocked for IE Chrome Safria, so an error will be reported.

The document. write method is blocking for all browsers, that is, synchronous. Therefore, alert ($) will output the correct result.

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.