Skip to content

Instantly share code, notes, and snippets.

@getify
Created July 27, 2011 19:46
Show Gist options
  • Save getify/1110207 to your computer and use it in GitHub Desktop.
Save getify/1110207 to your computer and use it in GitHub Desktop.
ugly hack to figure out the actual viewport dimensions (sans scrollbars)
// do the hack
document.childNodes[1].style.width = "100%"; // set the HTML to 100%/100%
document.childNodes[1].style.height = "100%";
document.body.style.width = "100%"; // set the <body> to 100%/100%
document.body.style.height = "100%";
var viewportDimsHack = this.treeBrowserDocument.createElement("div");
viewportDimsHack.style.position = "fixed";
viewportDimsHack.style.left = "0px";
viewportDimsHack.style.top = "0px";
viewportDimsHack.style.width = "100%";
viewportDimsHack.style.height = "100%";
document.body.appendChild(viewportDimsHack);
// get the viewport width/height
var viewportWidth = viewportDimsHack.offsetWidth;
var viewportHeight = viewportDimsHack.offsetHeight;
// undo the hack
document.body.removeChild(viewportDimsHack);
document.childNodes[1].style.width = "";
document.childNodes[1].style.height = "";
document.body.style.width = "";
document.body.style.height = "";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment