How Do I Get innerWidth in Internet explorer 8

Javascript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

for ie8 use

document.documentElement.clientWidth

Solution 4 - Javascript

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;

Solution 5 - Javascript

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.

Solution 6 - Javascript

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

Solution 7 - Javascript

document.documentElement.clientWidth;

this is used for ie8 to get the client width

Solution 8 - Javascript

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

jquery api documentation for .innerwidth()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionantonjsView Question on Stackoverflow
Solution 1 - JavascriptSarfrazView Answer on Stackoverflow
Solution 2 - JavascriptyckartView Answer on Stackoverflow
Solution 3 - JavascriptnicolasView Answer on Stackoverflow
Solution 4 - JavascriptKyle FalconerView Answer on Stackoverflow
Solution 5 - JavascriptJonView Answer on Stackoverflow
Solution 6 - JavascriptAmar PalsapureView Answer on Stackoverflow
Solution 7 - JavascriptKunal VashistView Answer on Stackoverflow
Solution 8 - JavascripticramcView Answer on Stackoverflow