window.innerHeight ie8 alternative

2019-01-17 00:56发布

问题:

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?

回答1:

You might want to try:

document.documentElement.clientHeight;

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

$(window).height();


回答2:

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


回答3:

Use following in your document.ready and define innerHeight explicitly.

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


回答4:

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



回答5:

I searched on because n o n e 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);