Home > Article > Web Front-end > js document.write() usage introduction_javascript skills
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.
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:
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.
colour1: ' colour1 '
colour2: ' colour2 '
The print result is as follows:
colour1: purple
colour2: pink
document.write is also mostly used to load js ads
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
答案:
对于动态创建的js 引用而言 ,针对不同的浏览器有不同的反应
楼主的这种写法 对 FF Opera 而言 load 方法是阻塞的 故alert($)能输出,而对IE Chrome Safria 而言 是非阻塞的 所以就会报错
而document.write的方式,对所有浏览器而言都是阻塞的 即同步的 所以alert($)会输出正确结果