Get browser viewport width and height


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. <!--
  3.  
  4. var viewportwidth;
  5. var viewportheight;
  6.  
  7. // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  8.  
  9. if (typeof window.innerWidth != 'undefined')
  10. {
  11. viewportwidth = window.innerWidth,
  12. viewportheight = window.innerHeight
  13. }
  14.  
  15. // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  16.  
  17. else if (typeof document.documentElement != 'undefined'
  18. && typeof document.documentElement.clientWidth !=
  19. 'undefined' && document.documentElement.clientWidth != 0)
  20. {
  21. viewportwidth = document.documentElement.clientWidth,
  22. viewportheight = document.documentElement.clientHeight
  23. }
  24.  
  25. // older versions of IE
  26.  
  27. else
  28. {
  29. viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
  30. viewportheight = document.getElementsByTagName('body')[0].clientHeight
  31. }
  32. document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
  33. //-->
  34. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.