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

前端 未结 3 2030
时光说笑
时光说笑 2020-12-17 02:54

I want to get exact height of the body of webbrowser window. I tried innerHeight, clientHeight all other solutions which I get while googling. but none of them give exact he

相关标签:
3条回答
  • 2020-12-17 03:10

    Some browsers report the window height incorrectly differently - particularly mobile browsers, which have a different viewport concept. I sometimes use a function to check several different values, returning whichever is the greatest. For example

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

    Edit: I just looked at how jQuery does it and it does indeed use Math.max and a series of properties - however the list it checks is slightly different to those in my example above, and since I usually trust the jQuery team to be better at this stuff than I am; here is the non-jQuery jQuery solution (if that makes any sense):

    function documentHeight() {
        return Math.max(
            document.documentElement.clientHeight,
            document.body.scrollHeight,
            document.documentElement.scrollHeight,
            document.body.offsetHeight,
            document.documentElement.offsetHeight
        );
    }
    
    0 讨论(0)
  • 2020-12-17 03:17

    You can use Jquery to achieve this...

    $(document).ready(function(){
        browserWindowheight = $(window).height();
        alert(browserWindowheight);
    });
    
    0 讨论(0)
  • 2020-12-17 03:34

    Have you tried using:

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

    For Internet Explorer with backward-compatibility mode, use

    document.body.offsetHeight 
    

    And also, Internet Explorer (standards mode, document.compatMode=='CSS1Compat'):

    document.documentElement.offsetHeight 
    

    Have a look at the following for more information:

    • http://www.javascripter.net/faq/browserw.htm
    • http://everyday-tech.blogspot.com.au/2009/07/js-calculate-browser-window-height.html
    0 讨论(0)
提交回复
热议问题