Skip to content

Instantly share code, notes, and snippets.

@yckart
Last active October 22, 2021 20:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save yckart/9128d824c7bdbab2832e to your computer and use it in GitHub Desktop.
Save yckart/9128d824c7bdbab2832e to your computer and use it in GitHub Desktop.
Get Viewport dimensions in all browsers. http://stackoverflow.com/a/18136089/1250044
(function (window, document) {
var html = document.getElementsByTagName('html')[0];
var body = document.getElementsByTagName('body')[0];
var ObjectDefineProperty = Object.defineProperty;
function define(object, property, getter) {
if (!(property in object)) {
ObjectDefineProperty(object, property, { get: getter });
}
}
define(window, 'innerWidth', function () { return html.clientWidth; });
define(window, 'innerHeight', function () { return html.clientHeight; });
define(window, 'scrollX', function () { return window.pageXOffset || html.scrollLeft; });
define(window, 'scrollY', function () { return window.pageYOffset || html.scrollTop; });
// OBSOLETE https://developer.mozilla.org/en-US/docs/Web/API/document.height
define(document, 'width', function () { return Math.max(body.scrollWidth, html.scrollWidth, body.offsetWidth, html.offsetWidth, body.clientWidth, html.clientWidth); });
define(document, 'height', function () { return Math.max(body.scrollHeight, html.scrollHeight, body.offsetHeight, html.offsetHeight, body.clientHeight, html.clientHeight); });
return define;
}(window, document));
@nhduong29
Copy link

thanks for your help :)
hope you have more tips to share for all people :)

@kostiantynkoval
Copy link

thank you! it really helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment