简体   繁体   中英

How Do I Get innerWidth in Internet explorer 8

In all recent browser:

 window.innerWidth // 1920

In Internet explorer 8

 window.innerWidth // undefined

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

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

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

The above line will get you the width from IE as well as other standard-compliant browsers.


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

For getting 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 imitate the real dom-getter take a look at this: https://stackoverflow.com/a/18136089/1250044

用于ie8使用

document.documentElement.clientWidth

I know that the innerWidth might not be quite the same, but getBoundingClientRect could also be used in a similar capacity:

elem = document.documentElement.getBoundingClientRect();
width = elem.getBoundingClientRect().right - elem.getBoundingClientRect().left;

You can get this information from IE 8 with

document.documentElement.clientWidth

Be advised that this value will not be exactly the same that IE 9 returns for window.innerWidth .

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;
}
document.documentElement.clientWidth;

这用于ie8以获取客户端宽度

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

jquery api documentation for .innerwidth()

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