How to get the inner width in Internet Explorer 8

In all recent browsers:

window.innerWidth // 1920 

In Internet Explorer 8

  window.innerWidth // undefined 

What is the best way to get this value in IE8?

+59
javascript
Feb 23 '12 at 9:19
source share
8 answers

innerWidth supported by IE9, not IE8, you can do this:

 var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; 

In the above line, you will get the width from IE, as well as other standard compatible browsers.




If you use jQuery, $(window).innerWidth() will also give you the desired result in all browsers.

+112
Feb 23 '12 at 9:24
source share

For this, I still use:

 window.innerWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; window.innerHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; 

But to mimic the real dom-getter, take a look at this: https://stackoverflow.com/a/2776262

+5
Jul 14 '13 at 11:45
source share

for ie8 use

 document.documentElement.clientWidth 
+4
Feb 14 '13 at 11:34
source share

I know that innerWidth may not be exactly the same, but getBoundingClientRect can also be used with similar capacity:

 elem = document.documentElement.getBoundingClientRect(); width = elem.getBoundingClientRect().right - elem.getBoundingClientRect().left; 
+4
Dec 18 '13 at 22:19
source share

You can get this information from IE 8 using

 document.documentElement.clientWidth 

Note that this value will not be exactly the same as IE 9 will return for window.innerWidth .

+2
Feb 23 '12 at 9:23
source share

Without jQuery, you can try this to get height and width

 var myWidth = 0, myHeight = 0; if (typeof (window.innerWidth) == 'number') { //Chrome myWidth = window.innerWidth; myHeight = window.innerHeight; } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight; } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { //IE9 myWidth = document.body.clientWidth; myHeight = document.body.clientHeight; } 
+1
Feb 23 2018-12-12T00:
source share
 document.documentElement.clientWidth; 

this is used for ie8 to get client width

0
Feb 23 '12 at 9:40
source share

Please note: the .innerWidth method is not applicable to window and document objects; for them use .width () instead.

jquery api documentation for .innerwidth ()

-one
Jun 28 '13 at 15:13
source share



All Articles