How Do I Get innerWidth in Internet explorer 8

前端 未结 8 1072
一整个雨季
一整个雨季 2020-12-08 00:06

In all recent browser:

 window.innerWidth // 1920

In Internet explorer 8

 window.innerWidth // undefined

相关标签:
8条回答
  • 2020-12-08 00:29
    document.documentElement.clientWidth;
    

    this is used for ie8 to get the client width

    0 讨论(0)
  • 2020-12-08 00:41

    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;
    
    0 讨论(0)
  • 2020-12-08 00:41

    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.

    0 讨论(0)
  • 2020-12-08 00:41

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

    jquery api documentation for .innerwidth()

    0 讨论(0)
  • 2020-12-08 00:49

    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

    0 讨论(0)
  • 2020-12-08 00:49

    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;
    }
    
    0 讨论(0)
提交回复
热议问题