This code will help you to get the browser's view port width & height.
First, we need to write the below JavaScript inside the
head
section in HTML.
<script type="text/javascript" language="javascript">
function fnGetWidthHeight() {
var viewportwidth;
var viewportheight;
if (typeof window.innerWidth != 'undefined') {
viewportwidth = window.innerWidth;
viewportheight = window.innerHeight;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
viewportwidth = document.documentElement.clientWidth;
viewportheight = document.documentElement.clientHeight;
}
else {
viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
viewportheight = document.getElementsByTagName('body')[0].clientHeight;
}
alert('Your viewport width & height is ' + viewportwidth + 'x' + viewportheight);
}
</script>
Then, we need to call this
script
from the
body
tag like below:
<body onload="fnGetWidthHeight()">
Now, run and see the size of view port area by increasing/decreasing the browser width/height.