advertisement -- please support sponsors

positioning new windows

How can I control the position of new browser windows created with the window . open method?

Although JavaScript's window . open method makes it possible to automatically open new browser windows and control many aspects of their appearance, the current version of Netscape Navigator (3.0) does not provide a way to control the browser windows' positions on the user's screen. This has frustrated many JavaScript programmers.

The Microsoft Corporation, however, has addressed this problem in it's own, proprietary version of JavaScript, called "JScript." (Implemented in the Internet Explorer, JScript is very similar to JavaScript, and sometimes even appears to be compatible. Unfortunately, there are many subtle differences in the features that both scripting languages support. What a wonderful way to keep the browser wars going! :-)

In both scripting languages, the window . open method takes the following form:

window . open (URL, window_name, window_features_list);
The first parameter is the URL of the document to be displayed in the new window; the second parameter is the name of the new window (used for frame and window targets); and the third parameter is a quoted, comma-separated list of window features. (All three parameters are strings.)

Although the window . open method works nearly the same in both scripting languages, JScript supports two features in the window features list that JavaScript does not: namely, the initial horizontal and vertical coordinates of the new window being opened. JScript calls these features "left" and "top," and they take the form

where hpixels and vpixels are the horizontal and vertical pixel coordinates of the upper-left corner of the new window.

Thus, if you want your JScript program to open a 400-by-200 pixel window in the upper-left corner of the users screen, you can do it on Internet Explorer with the following code:

window . open (location, name, "top=0,left=0,width=400,height=200");
Because Netscape Navigator simply ignores unrecognized features in the window features list, it is probably safe to use left and top regardless of your "target browser audience." In fact, since there is a pretty good chance that future versions of Netscape will support these features, it probably won't hurt to begin using them now. Remember, however, that at present they only work on Internet Explorer.

Charlton Rose
30 Dec. 1996

Editor's note: Netscape Navigator 4 now includes support for window positioning. Use screenY and screenX instead of top and left, respectively.