这令我感到沮丧。它应该是非常简单的东西,但我不能让它在IE中工作。我想得到当前窗口的高度:不是滚动高度,不是文档高度,而是实际窗口高度。我尝试了window.innerHeight
,它返回了undefined
和document.documentElement.clientHeight
,它给出了滚动高度。
答案 0 :(得分:74)
对于当前的浏览器
window.innerHeight
对于IE 8及更低版本,请使用
document.documentElement.offsetHeight;
如果您需要较旧的浏览器,请使用:
var height = "innerHeight" in window
? window.innerHeight
: document.documentElement.offsetHeight;
答案 1 :(得分:5)
我正在使用:
doc = document;
var theHeight = Math.max(
doc.body.scrollHeight, doc.documentElement.scrollHeight,
doc.body.offsetHeight, doc.documentElement.offsetHeight,
doc.body.clientHeight, doc.documentElement.clientHeight
);
在这里找到它: Get document height (cross-browser) - James Padolsey
并且还发现jQuery正在做同样的事情:
// Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest
// unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it.
return Math.max(
elem.body[ "scroll" + name ], doc[ "scroll" + name ],
elem.body[ "offset" + name ], doc[ "offset" + name ],
doc[ "client" + name ]
);
答案 2 :(得分:1)
<击> http://www.javascripter.net/faq/browserw.htm 击>
<击> 请注意,在浏览器解析了标记后,必须执行使用 document.body.offsetWidth
和document.body.offsetHeight
的代码。
更新: 试试这个
<script type="text/javascript">
<!--
var viewportwidth;
var viewportheight;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth,
viewportheight = document.documentElement.clientHeight
}
// older versions of IE
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
viewportheight = document.getElementsByTagName('body')[0].clientHeight
}
document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
//-->
</script>
找到here