Skip to content

Instantly share code, notes, and snippets.

@IOZ
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IOZ/8787497 to your computer and use it in GitHub Desktop.
Save IOZ/8787497 to your computer and use it in GitHub Desktop.
Get window viewport width and height
/**
* Get window viewport width
* @param side {string} - (width|height|both)
*
* Usage getViewPort('width'); // return viewport width
*/
function getViewPort(side){
var side = side || 'both';
var viewport = { width: 0, height: 0 };
if (typeof window.innerWidth != 'undefined') {
viewport.width = window.innerWidth;
viewport.height = 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) {
viewport.width = document.documentElement.clientWidth;
viewport.height = document.documentElement.clientHeight;
}
// older versions of IE
else {
viewport.width = document.getElementsByTagName('body')[0].clientWidth;
viewport.height = document.getElementsByTagName('body')[0].clientHeight;
}
switch(side){
case 'width':
return viewport.width;
break;
case 'height':
return viewport.height;
break;
case 'both':
return viewport;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment