Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Client Scripting
Expertise: Beginner
Mar 19, 1997

Can I control the position of new browser windows?

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

Answer:
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 JavaScript 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 its own, proprietary version of JavaScript, called "JScript." (Implemented in the Internet Explorer browser, 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

left=hpixels, top=vpixels
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 400x200-pixel window in the upper-left corner of the user's screen, you can do it in 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 in Internet Explorer.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date