JavaScript "screen" property with Dual screens

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

JavaScript "screen" property with Dual screens

Post by Chris Corbyn »

Is there a *clean* way to detect if someone is using dual screens in JS?

Basically I need to use DHTML to position something on a screen and have just discovered on my work machine which has two monitors at 1280 width each my calculation breaks since the screen.width property returns the size of the two monitors combined. Of course, nobody would ever being using both screens for a single window (and in Gnome I can't maximize if I do that) so I need to get the width of just the one screen.

I've written a function:

Code: Select all

function isDualScreen()
{
	var res = screen.width/screen.height;
	
	if (Math.abs((res - (4/3))) < Math.abs((res - (8/3))))
	{
		return false;
	}
	else
	{
		return true;
	}
}
But is there a better way to work it out? My code is basically seeing if it's closer to an 8/3 ratio than a 4/3 ratio and although it works it seems a bit fudgey.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What does screen.availHeight and screen.availWidth return?

There's also a possibility of using window.innerHeight and window.outerHeight.. but those are Mozilla specific, that I remember.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:What does screen.availHeight and screen.availWidth return?

There's also a possibility of using window.innerHeight and window.outerHeight.. but those are Mozilla specific, that I remember.
screen.availWidth and height do the same thing :(

Yeah, the innerWidth and innerHeight stuff doesn't work in IE which sucks.

Thanks anyway :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

FYI document.body.clientHeight and width are the IE equivalent of innerHeight and innerWidth. Just found that out myself :)
Post Reply