简体   繁体   中英

window.innerHeight ie8 alternative

I have some calculations that rely on window.innerHeight . But as I have found out this is not available in any IE before IE9. All the other options I have looked at don't even come close to the figure I get when I use window.innerHeight .

Does anybody have a work around?

You might want to try:

document.documentElement.clientHeight;

Or can use jQuery's .height() method:

$(window).height();

I made a simple shim for IE8 and others:

  • window.innerWidth
  • window.innerHeight
  • window.scrollX
  • window.scrollY
  • document.width
  • document.height

559 bytes

(function(d,b){var c=b.documentElement;var a=b.body;var e=function(g,h,f){if(typeof g[h]==="undefined"){Object.defineProperty(g,h,{get:f})}};e(d,"innerWidth",function(){return c.clientWidth});e(d,"innerHeight",function(){return c.clientHeight});e(d,"scrollX",function(){return d.pageXOffset||c.scrollLeft});e(d,"scrollY",function(){return d.pageYOffset||c.scrollTop});e(b,"width",function(){return Math.max(a.scrollWidth,c.scrollWidth,a.offsetWidth,c.offsetWidth,a.clientWidth,c.clientWidth)});e(b,"height",function(){return Math.max(a.scrollHeight,c.scrollHeight,a.offsetHeight,c.offsetHeight,a.clientHeight,c.clientHeight)});return e}(window,document));

For updates, take a look at this gist: yckart/9128d824c7bdbab2832e

(function (window, document) {

  var html = document.documentElement;
  var body = document.body;

  var define = function (object, property, getter) {
    if (typeof object[property] === 'undefined') {
      Object.defineProperty(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 });

  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));

document.ready使用以下document.ready并明确定义innerHeight

window.innerHeight = window.innerHeight || document.documentElement.clientHeight

Javascript method to get full window height..:

window.outerHeight;

Javascript methods to get full document height..:

document.body.clientHeight;

or

document.body.offsetHeight;

Javascript methods to get visible document area height..:

this is height of window without bars.

window.innerHeight;

jQuery methods to get visible document area height and window without bars area height too..:

$(window).height();

or

$(window).outerHeight();

jQuery methods to get full document height..:

$(document).height();

or

$(document).outerHeight();

that's all, I hope will help you :)

I searched on because none of the here posted functions seemed to work, i always got the windowviewheight not the documents full height...

this works in all browsers I've tested...also iexplore 8:

var D = document;
var myHeight = Math.max(
    D.body.scrollHeight, D.documentElement.scrollHeight,
    D.body.offsetHeight, D.documentElement.offsetHeight,
    D.body.clientHeight, D.documentElement.clientHeight
);

alert(myHeight); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM