如何获取网络浏览器窗口正文的确切高度

how to get exact height of body of the webbrowser window?

本文关键字:正文 窗口 高度 浏览器 网络 何获取 获取      更新时间:2023-09-26

我想获得网络浏览器窗口正文的确切高度。我尝试了innerHeight,clientHeight我在谷歌搜索时得到的所有其他解决方案。但它们都没有给出确切的高度。我不得不通过从这些功能提供的高度中减去一些值来调整高度??如何才能摆脱这个问题。我想我错过了一些东西。请帮忙。

谢谢

某些浏览器以不同的方式错误地报告窗口高度 - 特别是具有不同视口概念的移动浏览器。我有时会使用一个函数来检查几个不同的值,返回最大的值。例如

function documentHeight() {
    return Math.max(
        window.innerHeight,
        document.body.offsetHeight,
        document.documentElement.clientHeight
    );
}

编辑:我刚刚看了jQuery是如何做到的,它确实使用了Math.max和一系列属性 - 但是它检查的列表与我上面示例中的列表略有不同,并且因为我通常相信jQuery团队在这方面比我更好;这是非jQuery jQuery解决方案(如果这有任何意义的话):

function documentHeight() {
    return Math.max(
        document.documentElement.clientHeight,
        document.body.scrollHeight,
        document.documentElement.scrollHeight,
        document.body.offsetHeight,
        document.documentElement.offsetHeight
    );
}

你可以使用 Jquery 来实现这一点...

$(document).ready(function(){
    browserWindowheight = $(window).height();
    alert(browserWindowheight);
});

您是否尝试过使用:

window.outerHeight (this is for IE9 and other modern browser's height)

对于具有向后兼容模式的 Internet Explorer,请使用

document.body.offsetHeight 

还有Internet Explorer(标准模式,document.compatMode=='CSS1Compat'):

document.documentElement.offsetHeight 
有关详细信息

,请查看以下内容:

  • http://www.javascripter.net/faq/browserw.htm
  • http://everyday-tech.blogspot.com.au/2009/07/js-calculate-browser-window-height.html