2 Steps to get browser width and height in JavaScript



In article 2 Steps to know whether vistors use mobile to visit your website in PHP, we introduce a way by check the device type of visitors are using to display different scale page to visitors, however, we can check the width and height of  visitors’ browser to realize too, and this article will give some useful information to you.

Step 1: To get width and height of browser by JavaScript

To get width and height by javascript is not hard, we can use code below to realize it.


<script>
var w=window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var h=window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

alert("w="+w+ " h="+h);
</script>

Then we will run this code to know width and height.

Step 2: Analysis the result

Run code above we will get a result like below:

get browser width and height

From this picture, we will know we get the width of height of displaying content, not the width and height of the whole browser.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>