A tweet-sized & cross-browser way to get the actual document size (document.height
and document.width
). This piece of web-thing was originally created by James Padolsey (http://james.padolsey.com/javascript/get-document-height-cross-browser/), I've golfed it just down to 139 Bytes.
-
-
Save yckart/10988011 to your computer and use it in GitHub Desktop.
Getting the document width and height in less than 140 bytes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "getDocumentSize", | |
"description": "Gets the document width or height, in a cross-browser manner.", | |
"keywords": [ | |
"document", | |
"width", | |
"height", | |
"size", | |
"dimensions" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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