spacer
Yehuda Shiran June 4, 2000
Writing to a New Window
Tips: June 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The following script shows a typical scenario for opening a window and writing a new document to it:

var win = window.open("", "win", "width=300,height=200"); // a window object
win.document.open("text/html", "replace");
win.document.write("<HTML><HEAD><TITLE>New Document</TITLE></HEAD><BODY>Hello, world!</BODY></HTML>");
win.document.close();

The first statement opens a new window with an empty document (""). The returned value is assigned to a variable named win. We then use the new window's document object, win.document, to write some HTML to the new window. The "replace" specification is necessary, because we don't want the blank page to have an entry in the History list.

Since we're dealing with the same document object, we might as well assign it to another variable:

var win = window.open("", "win", "width=300,height=200"); // a window object
var doc = win.document;
doc.open("text/html", "replace");
doc.write("<HTML><HEAD><TITLE>New Document</TITLE></HEAD><BODY>Hello, world!</BODY></HTML>");
doc.close();

We can also use the with statement:

var win = window.open("", "win", "width=300,height=200"); // a window object
with (win.document) {
  open("text/html", "replace");
  write("<HTML><HEAD><TITLE>New Document</TITLE></HEAD><BODY>Hello, world!</BODY></HTML>");
  close();
}

Click the following button to run the script:

Learn more about manipulating windows in Tutorial 1, Working with Windows.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business