Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to get the Display Dimensions of my Monitor..

Is it possible to do with JavaScript of Flash?

I tried the following function:

var dpi_x = document.getElementById('testdiv').offsetWidth;
        var dpi_y = document.getElementById('testdiv').offsetHeight;
        var width_in = screen.width / dpi_x;
        var height_in = screen.height / dpi_y;
        var diagonal_in = Math.round(10 * Math.sqrt(width_in * width_in + height_in * height_in)) / 10;


but it doesnt shows the fixed diagonal value..because when i change the resolution of display..

Where as i want the fixed dimension like 16.5"....i mean it should be fixed for any specific monitor..
Posted
Updated 20-Aug-12 23:16pm
v2
Comments
Ravi Tuvar 21-Aug-12 4:30am    
i dont want it based on screen resolution..cause when i change the resolution it varies..
enhzflep 21-Aug-12 5:19am    
A guess - you could set the style of an object such that (say) it's height is fixed with css at 1cm. You could then get the height of the element with JavaScript (in pixels.) This will allow you to get the pixels/cm value that you need.

JavaScript
use below code

screen.width;
screen.height;
 
Share this answer
 
Comments
Ravi Tuvar 21-Aug-12 5:29am    
hi Prashant,

Thanks for your reply..
but This value will be variable when i change the resolution..

What i want is the fixed Monitor's dimension(size)..
Have a try on querying Win32_DesktopMonitor[^] using WMI.
 
Share this answer
 
Here you go, this will do it.

My screen is 13.5 x 7.5 inches - it's a 15.3" screen.
This (wrongly) calculates it as 14.22 x 8 - or 16.3"
Don't know why it gives the wrong result - it gives the dpi as 96 which sounds right and checks out in my display settings. (** See edit below for update **)

As I hinted earlier, I've just set the size of an element to be 1"x1" I then get it's size in pixels and calculate the screen's physical size based on the pixels/inch and the screen resolution.

As screen resolution changes, the size returned changes if the screen-mode has black vertical bars on the sides of the screen. That's to say it measures the size of the screen that contains content. I set 1024*768 and got ~ 150pixels of black bars on either side of the screen. The vertical size stayed the same, but the horizontal & diagonal dimensions shrunk.

HTML
<!DOCTYPE html>
<html>
<style>
#myDiv
{
	width: 1in;
	height: 1in;
	padding: none;
	margin: none;
	background-color: black;
}
</style>
<script>

function getSize()
{
	var tgt = document.getElementById('myDiv');
	tgtX = tgt.clientWidth;//offsetWidth;
	tgtY = tgt.clientHeight;//offsetHeight;
	
	var width_in = screen.width / tgtX;
    var height_in = screen.height / tgtY;
	
	width_in = parseInt(width_in * 100);
	width_in /= 100;
	
	diag = Math.sqrt( width_in*width_in + height_in*height_in);
	
	//alert('Div size: ' + tgtX + "x" + tgtY + " px.");
	alert('Screen size: ' + width_in + "\" x " + height_in + "\"  - Diagonal: " + diag + "\"");
}
</script>
</head>
<body onload='getSize();'>
	<div id='myDiv'></div>
<body>
</html>


[EDIT] Setting the width and height to 2.7cm instead of 1in means the square measures 1inch on my screen. This then gives more or less the right results. 13.39" x 7.52" (15.36" diag)
 
Share this answer
 
v4
Comments
Ravi Tuvar 23-Aug-12 5:06am    
I have used the same concept but as i said..whenever i change the resolution the Height, Width and Diagonal changed...in fact the Value must be fixed since Display device is a hardware
enhzflep 23-Aug-12 5:26am    
Yeah, but the thing is that the display size does change in many cases. While the panel itself is still the same size, the display is no longer using all of it - that's why I get reduced values when I set 1024*768 - the image can't be scaled to fit 1366x768 well, so the extra 300 odd pixels that the panel has, are excluded from the display.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900