SoFunction
Updated on 2025-04-06

The little thing in js

Remember that after the page is loaded, the browser output stream is automatically closed. After this, any () method that operates on the current page will open a new output stream, which will clear the content of the current page (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() method to complete the write operation. Without clearing the document and opening a new data stream, all operations can be accomplished with a() call.

Another point to be explained about the() method is its related method (). After the script writes the content to the window (whether this or other window), the output stream must be closed. After the last() method of the delay script, you must make sure that the() method is included. If you do not do so, the image and form cannot be displayed. Moreover, any subsequently called() method will only append the content to the page, without clearing the existing content to write the new value. To demonstrate the() method, we provide two versions of the same application. One writes content to a document containing the script, and the other writes content to a separate window. Please click on each document in the text editor, save it with the .html file extension, and open the document in the browser.

Example 1 creates a button that combines new HTML content for the document, including the HTML tags for the new document title and the color attributes of the tag. In the example, there is an operator += that is not familiar to readers. It adds the string on its right to the variable on its left. This variable is used to store the string. This operator can easily combine several separate statements into a long string. Using the content combined in the newContent variable, the () statement can write all new content into the document, completely clearing the content in Example 1. Then you need to call the() statement to close the output stream. When loading the document and clicking the button, you can notice that the document title in the browser's title bar changes as a result. When you go back to the original document and click the button again, you can see that the second page written dynamically loads even faster than reloading the original document.

Example 1 Use () in the current window.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"><html xmlns="http:///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
  (newContent)
  () // 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 child 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 a property to the third parameter of the() method to make the status bar of the child window visible.

The button on the page calls the subWrite() method, and the first task it performs is to check the closed properties of the child window. If the reference window is closed, this property (only present in newer browser versions) returns true. If this is the case (if the user manually closes the window), the function calls the makeNewWindow() function again to reopen the window.

After the window opens, the new content is combined as a string variable. As in Example 1, write content at once (although it is not necessary for a separate window), and then call the close() method. But note that one important difference: both write() and close() methods clearly specify child windows.

Example 2 Using() in another window

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"><html xmlns="http:///1999/xhtml"><title>Writing to Subwindow</title>
<script language="JavaScript">
 var newWindow
 function makeNewWindow(){
  newWindow = ("","","status,height=200,width=300")
 }

 function subWrite(){
  // make new window if someone has closed it
  if(){
   makeNewWindow()
  }
  // bring subwindow to front
  ()
  // 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
  (newContent)
  ()  // close layout stream
 }
</script>
</head>

<body onLoad="makeNewWindow()">
 <form>
  <input type="button" value="Write to Subwindow" onClick="subWrite()">
 </form>
</body>
</html>