Home > Web Front-end > JS Tutorial > js document.write() usage introduction_javascript skills

js document.write() usage introduction_javascript skills

WBOY
Release: 2016-05-16 16:58:39
Original
1807 people have browsed it

After loading the 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 ​​from the source document), so. If you want to replace the current page with HTML generated by a script, you must concatenate the HTML content and assign it to a variable and use a document.write() method to complete the writing operation. You do not have to clear the document and open a new data stream. A document.write() ) can be called to complete all operations.

One more thing to note about the document.write() method is its related method document.close(). After the script finishes writing content to the window (whether it is this window or another window). The output stream must be closed. After the last document.write() method of the delay script. You must ensure that the document.close() method is included, otherwise pictures and forms will not be displayed. Also, any subsequent calls to the document.write() method will only append the content to the page, but will not clear the existing content to write new values.

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 verbatim, add single quotes around the printed text string.

Copy code 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. Enter the variable name without quotation marks, as follows:

Copy code The code is as follows:

var mytext = "Hello again";
document .write(mytext);

Note: If the variable name is enclosed 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.

Copy code The code is as follows:

var color1 = "purple";
var color2 = "pink";
document.write('

colour1: ' colour1 '
colour2: ' colour2 '

');

The print result is as follows:

colour1: purple
colour2: pink

document.write is also mostly used to load js ads

Copy code The code is as follows:

document.write('');
document.write("");
document.write("");

Generally, single quotes (double quotes) are used to connect characters outside, and double signals (single quotes) are used inside, so that there is no mistake. Of course, you can also use escape characters, but this will be more troublesome to modify later.

The js loaded in document.write is asynchronous

Copy code The code is as follows:




   
       
   
   
    ');
    }  

           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><br>alert($);<br></script>

问: 用createElement("script")的方式加载,调用函数就会报错,用document.write就不报错?

答案:

对于动态创建的js 引用而言 ,针对不同的浏览器有不同的反应  
楼主的这种写法 对 FF Opera 而言 load 方法是阻塞的 故alert($)能输出,而对IE Chrome Safria 而言 是非阻塞的 所以就会报错 
而document.write的方式,对所有浏览器而言都是阻塞的 即同步的 所以alert($)会输出正确结果

Related labels:
js
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template