After the manned page, the browser output stream is automatically closed; after that, any document.write() method that operates on the current page will open a new output stream. It will clear the current page content (including any variables or values of the source document), therefore. If you want to replace the current page with the HTML generated by the script, you must connect the HTML content and assign it to a variable, and use a document.write() method to complete the write operation. There is no need to clear the document and open a new data stream, a document.write( ) calls can complete all operations.
Another thing to be explained about the document.write() method is its related method document.close(). After the script writes the content to the window (whether this window or other windows). The output stream must be turned off. After the last document.write() method of the delay script. You must make sure that the document.close() method is included, and that pictures and forms cannot be displayed without doing so. And, any subsequently called document.write() method will only append the content to the page without clearing the existing content to write the new value.
document.write method
One of the most basic JavaScript commands is document.write. This command simply prints the specified text content to the page. To print text word by word, add single quotes to the printed text string.
The code copy is as follows:
document.write('Hello World!');
The above js code will show "Hello World!" on the page.
You can print variables using document.write. Enter the variable name without quotes, as follows:
The code copy is as follows:
var mytext = "Hello again";
document.write(mytext);
Note: If the variable name is given in quotes, the variable name will be printed (the variable value will not be printed). You can use the "+" symbol to concatenate variable values and text strings.
The code copy is as follows:
var colour1 = "purple";
var colour2 = "pink";
document.write('<p>colour1: ' + colour1 + '<br>colour2: ' + colour2 + '</p>');
The printing results are as follows:
colour1: purple
colour2: pink
document.write is also mostly used to load js ads
The code copy is as follows:
document.write('<script+'pt src="//www.VeVB.COM/ad.js" type="text/javascript"></s'+'script>');
document.write("<script"+"pt src='//www.VeVB.COM/ad.js' type='text/javascript'></s"+"script>");
document.write("<script"+"pt src=/"//www.VeVB.COM/ad.js/" type=/"text/javascript/"></s"+"script>");
Generally, characters are connected with single quotes (double quotes) outside, and those inside should be connected with double signals (single quotes), so that you will not be wrong. Of course, escape characters can also be used, but it is more troublesome to modify them later.
The js loaded in document.write is asynchronous
The code copy 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("//www.VeVB.COM/js/2011/jquery-1.5.1.min.js");
// write("//www.VeVB.COM/js/2011/jquery-1.5.1.min.js");
</script>
<script>
alert($);
</script>
Question: If you load using createElement("script"), you will report an error when calling the function. If you use document.write, you will not report an error?
Answer:
For dynamically created js references, different responses are available for different browsers
The author's writing method of FF Opera is blocking, so alert($) can output, but for IE Chrome Safria it is non-blocking, so it will report an error.
The document.write method is blocking for all browsers, that is, synchronous, so alert($) will output the correct result