Home >Web Front-end >JS Tutorial >Things about document.write in js_basic knowledge
Remember, the browser output stream is automatically closed after the page is loaded. After this, any document.write() method that operates on the current page will open a new output stream, which will clear the current page content (including any variables or values from the source document). Therefore, 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. Instead of clearing the document and opening a new data stream, a single document.write() call can do it all.
Another 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, images and forms cannot 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. To demonstrate the document.write() method, we provide two versions of the same application. One writes to the document containing the script, and the other writes to a separate window. Please type each document in a text editor, save it with a .html file extension, and open the document in your browser.
Example 1 creates a button that assembles new HTML content for the document, including an HTML tag for the new document title and a color attribute for the tag. In the example, there is an operator = that is unfamiliar to readers. It adds the string on the right side to the variable on the left side. This variable is used to store strings. This operator can easily combine several separate Statements are combined into a long string. Using the content combined in the newContent variable, the document.write() statement can write all new content to the document, completely clearing the content in Example 1. Then you need to call the document.close() statement to close the output stream. When the document is loaded and the button is clicked, notice that the document title in the browser title bar changes as a result. When you go back to the original document and click the button again, you can see that the dynamically written second page loads even faster than reloading the original document.
Example 1 uses document.write() in the current window.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><title>Writing to Same Doc</title> <script language="JavaScript"> function reWrite(){ // assemble content for new window var newContent = "<html><head><title>A New Doc</title></head>" newContent += "<body bgcolor='aqua'><h1>This document is brand new.</h1>" newContent += "Click the Back button to see original document." newContent += "</body></html>" // write HTML to new window document document.write(newContent) document.close() // close layout stream } </script> </head> <body> <form> <input type="button" value="Replace Content" onClick="reWrite()"> </form> </body> </html>
In Example 2, the situation is a bit complicated because the script creates a sub-window into which the entire document generated by the script will be written. To keep the reference to the new window active in both functions, we declare the newWindow variable as a global variable. When the page is loaded, the onLoad event handler calls the makeNewWindow() function, which generates an empty child window. In addition, we add an attribute to the third parameter of the window.open() method to make the status bar of the child window visible.
The button on the page calls the subWrite() method. The first task it performs is to check the closed attribute of the subwindow. This property (only present in newer browser versions) returns true if the reference window is closed. If this is the case (if the user manually closes the window), the function calls makeNewWindow() again to reopen that window.
After the window is opened, the new content is combined as a string variable. As in Example 1, the content is written once (although this is not necessary for a separate window), and then the close() method is called. But note an important difference: both the write() and close() methods explicitly specify child windows.
Example 2 Using document.write() in another window
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><title>Writing to Subwindow</title> <script language="JavaScript"> var newWindow function makeNewWindow(){ newWindow = window.open("","","status,height=200,width=300") } function subWrite(){ // make new window if someone has closed it if(newWindow.closed){ makeNewWindow() } // bring subwindow to front newWindow.focus() // assemble content for new window var newContent = "<html><head><title>A New Doc</title></head>" newContent += "<body bgcolor='coral'><h1>This document is brand new.</h1>" newContent += "</body></html>" // write HTML to new window document newWindow.document.write(newContent) newWindow.document.close() // close layout stream } </script> </head> <body onLoad="makeNewWindow()"> <form> <input type="button" value="Write to Subwindow" onClick="subWrite()"> </form> </body> </html>