Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Last active August 29, 2015 13:59
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 yckart/10988011 to your computer and use it in GitHub Desktop.
Save yckart/10988011 to your computer and use it in GitHub Desktop.
Getting the document width and height in less than 140 bytes.
function (
a // The dimension-method, can be either `"Width"` or `"Height"`
) {
var
b = document, // the whole `document`, html`& `body` are properties of it
c = b.documentElement, // `html`-node
d = b.body, // `body`-node
e; // placeholder, for the composited property. ("scroll" + "Height" = "scrollHeight")
return Math.max( // returns the number with the highest value
d[
e = "scroll" + a // we assign `"scroll" + method` ("Height" or "Width")
], // and get the value from previour assigned property from `body`
c[e], // ...and from `html`
// At this point I don't want to repeat myself...
// All we do here, is already explained one row above ;-)
d[e = "offset" + a],
c[e],
d[e = "client" + a],
c[e]
)
}
function(a){var b=document,c=b.documentElement,d=b.body,e;return Math.max(d[e="scroll"+a],c[e],d[e="offset"+a],c[e],d[e="client"+a],c[e])}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2014 Yannick Albert <http://yannick-albert.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "getDocumentSize",
"description": "Gets the document width or height, in a cross-browser manner.",
"keywords": [
"document",
"width",
"height",
"size",
"dimensions"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>2000</b></div>
<div>Actual value: <b id="ret"></b></div>
<style>html, body { margin: 0; padding:0; height: 2000px }</style>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var getDocumentSize = function(a){var b=document,c=b.documentElement,d=b.body,e;return Math.max(d[e="scroll"+a],c[e],d[e="offset"+a],c[e],d[e="client"+a],c[e])}
document.getElementById( "ret" ).innerHTML = getDocumentSize("Height")
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment