Posted on Tuesday, 18th August 2009 by chris
<script type="text/javascript"> <!-- var viewportwidth; var viewportheight; // the more standards compliant browsers (mozilla/netscape/opera/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 } document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>'); //--> </script>
Posted in JavaScript | Comments (3)
3 Responses to “Get viewport size (width and height) with javascript”
Leave a Reply
You must be logged in to post a comment.
August 19th, 2009 at 11:43 PM
I liked it. So much useful material. I read with great interest.
August 21st, 2009 at 12:02 PM
Interesting and informative. But will you write about this one more?
August 24th, 2009 at 6:58 AM
Thank you! You often write very interesting articles. You improved my mood.