advertisement -- please support sponsors

compatibility, dynamic content, windows:
determining the user's screen size

I am working on a site that requires a new window open with now tool bar. I know how to open a new window and sett all the attributes, but I have a problem with the height and width. Currently I can not put % in. I would like my new window to take up the whole size of the screen. Unless I hard code it, there is no way I can do it. Is there a way I can get screen resolution of the user and set the height and width according to the resolution?

-- Arya

A lot of people have been asking this question. JavaScript does not provide utilities for directly accessing the user's screen parameters, but it does give you access to the standard Java classes, which in turn can be used to determine the user's screen size.

The Java class java . awt . Toolkit has a static method called "getDefaultToolkit ()." This method can be used to obtain the default Toolkit object for the user's system, which contains information about the user's specific machine. Get the toolkit with the following code:

<script>

toolkit = java . awt . Toolkit . getDefaultToolkit ();
     .
     .
     .
After you have obtained the default toolkit, call its getScreenSize () method. The return value will be a Dimension object containing the user's screen size:
     .
     .
     .
screensize = toolkit . getScreenSize ();
     .
     .
     .
This dimension object has two members: width and height. Both are integers and represent, in pixels, the size of the user's screen:
     .
     .
     .
document . writeln
(
	"<p>" +
	"You screen is " + screensize . width + " pixels wide." +
	"</p>"
);
document . writeln
(
	"<p>" +
	"You screen is " + screensize . height + " pixels tall." +
	"</p>"
);

</script>
Putting the above script fragments together produces the following results on your browser:
Because scrolling a browser vertically is less of a nuisance than scrolling it horizontally, there may be situations in which you only care about the user's screen width. In this case, you use the following shortcut:
width = java
	. awt
	. Toolkit
	. getDefaultToolkit ()
	. getScreenSize ()
	. width;
The fact that JavaScript can access Java classes means that JavaScript is much more powerful than a mere scripting language. In fact, I'll bet one could even use Java classes to find out the user's color depth and monitor size!

The technique describe here shows how to obtain the user's screen size, not her browser size. Starting with Netscape 4, browser dimensions will be available as properties of the window object. The proposed names for these properties are: innerWidth, innerHeight, outerWidth, and outerHeight. The meaning of these variables ought to be fairly intuitive.

Charlton Rose
April 26, 1997