Click here to Skip to main content
12,066,448 members (57,143 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

Browsers View port Width & Height using Javascript

, 19 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
To get Browsers View port Width & Height using JavaScript
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;
            // The more standards compliant browsers (mozilla/netscape/opera/chrome/IE7) use window.innerWidth and window.innerHeight
            if (typeof window.innerWidth != 'undefined') {
                viewportwidth = window.innerWidth;
                viewportheight = window.innerHeight;
            // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
            else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
                viewportwidth = document.documentElement.clientWidth;
                viewportheight = document.documentElement.clientHeight;
            // older versions of IE
            else {
                viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
                viewportheight = document.getElementsByTagName('body')[0].clientHeight;
            alert('Your viewport width & height is ' + viewportwidth + 'x' + viewportheight);
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.


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


About the Author

Toniyo Jackson
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

Generalnice script Pin
_Programmer26-Apr-11 4:53
member_Programmer26-Apr-11 4:53 
GeneralRe: Thank You Pin
Toniyo Jackson26-Apr-11 20:58
memberToniyo Jackson26-Apr-11 20:58 
GeneralCool. +5 for sharing the Tip. Pin
Kunal_Chowdhury20-Apr-11 9:01
mvpKunal_Chowdhury20-Apr-11 9:01 
GeneralRe: Thanks Kunal Pin
Toniyo Jackson20-Apr-11 21:26
memberToniyo Jackson20-Apr-11 21:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 19 Apr 2011
Article Copyright 2011 by Toniyo Jackson
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid