Get Device Screen Width and Height using javaScript


To get the height and width of the Display Screen we must use screen.height and screen.width javascript properties respectively. The screen property is available in the Window Object, we don't need to prefix it with the screen though.

screen dimentions output
screen dimentions output
getScreenDimentions.html
<!DOCTYPE html>
<html>
<title>Get Device width and Height using Javscript</title>

<script type="text/javascript">

function getScreenDimentions() {
document.getElementById("dimenstions").innerHTML = 
"Display Width : " + screen.width +"<br/> Display Height : " + screen.height;
}
</script>


<h2>You Monitor display pixels</h2> 
<body onload="getScreenDimentions()">

<div id="dimenstions"></div>

</body>
</html>

To demonstrate we create an javaScript function in the head section getScreenDimentions() and set the screen width and height to the HTML div element with class name dimensions on body load.

Note that the screen property returns the dimensions in the unit pixel.

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap