open / close method of window and document objects

This method is used to open a stream to collect the output from any write or writeln methods. The first of the optional parameters is mimeType which determines the type of document you are writing to; if this parameter is not used, the default value is "text/html". The second parameter is replace, also optional, which causes the history entry for the new document to inherit the history entry from the document from which it was opened.

An important point about the open() method is that it is almost always invoked as window.open(), even though window refers to the global object and should therefore be entirely optional.

Since the document object also has an open() method, specifying the window object when we want to open a new window is essential for clarity.

A call to open() without specifying an object name is equivalent to document.open(). Thus, if such an event handler refers to the open() method, this identifier ends up being resolved in the Document object, and the event handler opens a new document rather than opening a new window.

function newDocumentTest() { //document.writeln("this is what happens not using the open/close method... ") //document.writeln("this is more of what happens not using the open/close method") var message1 = "WebDev 11 " var message2 = "This is a test." document.open() document.writeln(message1) document.write(message2) document.close() //document.writeln("this is what happens not using the open/close method... ") //document.writeln("this is more of what happens not using the open/close method") } function newWindowTest() { newWindow=window.open('','','toolbar=no,scrollbars=no,width=200,height=150') //newWindow.document.writeln("this is what happens not using the open/close method... ") //newWindow.document.writeln("this is more of what happens not using the open/close method") var message1 = "WebDev 11 " var message2 = "This is a test." newWindow.document.open("text/html", "replace") newWindow.document.writeln(message1) newWindow.document.write(message2) //document.write(" opps.. forgot to specify the window... ") newWindow.document.close() //newWindow.document.writeln("this is what happens not using the open/close method... ") //newWindow.document.writeln("this is more of what happens not using the open/close method") }