Create a new file called open-window.html and enter the following text:
<HTML><HEAD>
<TITLE>A window to the world!</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function DoOpen() {
w = window.open( "", "MyWindow", "width=480,height=210");w.document.write("<HEAD><TITLE>Hello!</TITLE></HEAD>");
w.document.write("<H1>This is so cool!</H1>");
w.document.write("<P>Hello, world!");w.document.close();
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#ffffff">
<FORM>
<INPUT TYPE="button" NAME="open" VALUE="Open!" ONCLICK="DoOpen()"></FORM>
</BODY>
</HTML>
Use your web browser to view this web page. Click the Open! button. What happens?
There are several new JavaScript concepts in this web page. The window object is asked to open a new window and assign it to w. The first parameter is the URL to load into the new window (we load nothing). The second parameter is the name of the window. The third parameter specifies some characteristics of the window, like the size. The variable w is a window object in its own right. And so we can tell its document to write some text inside itself. This text is HTML code, which will create a new web page on the fly!
There are a few things to be aware of when you open a secondary window using JavaScript. The parameters of the window method may not contain any blanks. If there are, the window will not open, and you may not even receive any error messages! And be sure to use the close method to make sure that everything is written to the window.
Change this web page to open a window and write a variety of text into it. Include several HTML tags.