Can I control the position of new browser windows?

Question:
How can I control the position of new browser windows createdwith the window . open method?

Answer:
Although JavaScript’s window . open method makes itpossible to automatically open new browser windows and control manyaspects of their appearance, the current version of JavaScript does not provide a way to control the browser windows’ positions on theuser’s screen. This has frustrated many JavaScript programmers.

The Microsoft Corporation, however, has addressed this problem in itsown, proprietary version of JavaScript, called “JScript.”(Implemented in the Internet Explorer browser, JScript is verysimilar to JavaScript, and sometimes even appears to be compatible.Unfortunately, there are many subtle differences in the features thatboth scripting languages support. What a wonderful way to keep thebrowser wars going! 🙂

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

window . open (URL,window_name,window_features_list);

The first parameter is the URL of the document to be displayed in thenew window; the second parameter is the name of the new window (used forframe and window targets); and the third parameter is a quoted,comma-separated list of window features. (All three parameters arestrings.)

Although the window . open method works nearly the same inboth scripting languages, JScript supports two features in the windowfeatures list that JavaScript does not ? namely, the initial horizontaland vertical coordinates of the new window being opened. JScript callsthese features “left” and “top,” and they takethe form

left=hpixels, top=vpixels

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

Thus, if you want your JScript program to open a 400×200-pixel windowin the upper-left corner of the user’s screen, you can do it inInternet Explorer with the following code:

window . open (location, name,"top=0,left=0,width=400,height=200");

Because Netscape Navigator simply ignores unrecognizedfeatures in the window features list, it is probably safe to useleft and top regardless of your “targetbrowser audience.” In fact, since there is a pretty good chance thatfuture versions of Netscape will support these features, itprobably won’t hurt to begin using them now. Remember, however, that atpresent they only work in Internet Explorer.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: