Developer

Learn how to write to a window

The content of a subwindow does not need to reside in its own HTML file. Find out how you can write to a window.

By Brent Lee Metcalfe

The content of a subwindow does not need to reside in its own HTML file. Let me illustrate.

The content for this example, including the graphic, was written to the window when the window was opened. There are seemingly innumerable ways to do this. Here's one:

var writeWin = null;
function writeLeft() { writeWin =
window.open('','aWin','top=0,left=0,width=250,height=350'); var ePen =
'<html><head><title>sans .html file</title></head>'; ePen += '<body
text="#cccccc" bgcolor="#000000"><tt><p>once upon a time, a ghost
said:</p>'; ePen += '<p><center><img
src="graphics/boo.jpg"></center></p><p>the end.</p></body></html>';
var wd = writeWin.document;
wd.open();
wd.write(ePen);
wd.close();
}

After defining the window (writeWin) in function writeLeft(), variable ePen is set as an HTML string. Additional strings are concatenated using the add-by-value operator (+=). Then a new document is initialized in window writeWin using the document open() method. The variable ePen is passed to the document write() method, which writes out the concatenated strings.

Note that you must use the document close() method to end the output stream or the document will not display.

Brent Lee Metcalfe is a Web architecture consultant.

Editor's Picks