In all recent browser:
在最近的所有浏览器:
window.innerWidth // 1920
In Internet explorer 8
在Internet explorer 8
window.innerWidth // undefined
What is the best way to get this value in IE8?
在IE8中获得这个值的最好方法是什么?
8 个解决方案
#1
111
The innerWidth
is supported by IE9 not IE8, you can do this insteaad:
内置宽度是由IE9而非IE8支持的,你可以这样安装:
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.
上面的代码将使您从IE和其他标准兼容的浏览器中获得宽度。
If you use jQuery, $(window).innerWidth()
will give you desired result in all browsers too.
如果您使用jQuery,那么在所有浏览器中,$(窗口). innerwidth()也将为您提供所需的结果。
#2
5
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://*.com/a/18136089/1250044
但是要模仿真正的dom getter,请看看这个:https://*.com/a/18136089/1250044。
#3
4
for ie8 use
ie8使用
document.documentElement.clientWidth
#4
4
I know that the innerWidth
might not be quite the same, but getBoundingClientRect
could also be used in a similar capacity:
我知道内宽可能不太一样,但是getBoundingClientRect也可以用于类似的容量:
elem = document.documentElement.getBoundingClientRect();
width = elem.getBoundingClientRect().right - elem.getBoundingClientRect().left;
#5
2
You can get this information from IE 8 with
你可以从IE 8获取这些信息
document.documentElement.clientWidth
Be advised that this value will not be exactly the same that IE 9 returns for window.innerWidth
.
请注意,该值将与IE 9返回windows . innerwidth的值不完全相同。
#6
1
Without jQuery you can try this to get height
and width
没有jQuery,您可以尝试获取高度和宽度
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;
}
#7
0
document.documentElement.clientWidth;
this is used for ie8 to get the client width
这用于ie8获取客户端宽度。
#8
-1
Please note: .innerWidth method is not applicable to window and document objects; for these, use .width() instead.
请注意:.innerWidth方法不适用于窗口和文档对象;对于这些,使用.width()代替。
jquery api documentation for .innerwidth()
.innerwidth()的jquery api文档
#1
111
The innerWidth
is supported by IE9 not IE8, you can do this insteaad:
内置宽度是由IE9而非IE8支持的,你可以这样安装:
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.
上面的代码将使您从IE和其他标准兼容的浏览器中获得宽度。
If you use jQuery, $(window).innerWidth()
will give you desired result in all browsers too.
如果您使用jQuery,那么在所有浏览器中,$(窗口). innerwidth()也将为您提供所需的结果。
#2
5
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://*.com/a/18136089/1250044
但是要模仿真正的dom getter,请看看这个:https://*.com/a/18136089/1250044。
#3
4
for ie8 use
ie8使用
document.documentElement.clientWidth
#4
4
I know that the innerWidth
might not be quite the same, but getBoundingClientRect
could also be used in a similar capacity:
我知道内宽可能不太一样,但是getBoundingClientRect也可以用于类似的容量:
elem = document.documentElement.getBoundingClientRect();
width = elem.getBoundingClientRect().right - elem.getBoundingClientRect().left;
#5
2
You can get this information from IE 8 with
你可以从IE 8获取这些信息
document.documentElement.clientWidth
Be advised that this value will not be exactly the same that IE 9 returns for window.innerWidth
.
请注意,该值将与IE 9返回windows . innerwidth的值不完全相同。
#6
1
Without jQuery you can try this to get height
and width
没有jQuery,您可以尝试获取高度和宽度
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;
}
#7
0
document.documentElement.clientWidth;
this is used for ie8 to get the client width
这用于ie8获取客户端宽度。
#8
-1
Please note: .innerWidth method is not applicable to window and document objects; for these, use .width() instead.
请注意:.innerWidth方法不适用于窗口和文档对象;对于这些,使用.width()代替。
jquery api documentation for .innerwidth()
.innerwidth()的jquery api文档