How to get document height and width without using jquery

后端 未结 10 1346
有刺的猬
有刺的猬 2020-11-28 05:26

How to get document height and width in pure javascript i.e without using jquery.
I know about $(document).height() and $(document).width(), bu

相关标签:
10条回答
  • 2020-11-28 05:45

    Even the last example given on http://www.howtocreate.co.uk/tutorials/javascript/browserwindow is not working on Quirks mode. Easier to find than I thought, this seems to be the solution(extracted from latest jquery code):

    Math.max(
        document.documentElement["clientWidth"],
        document.body["scrollWidth"],
        document.documentElement["scrollWidth"],
        document.body["offsetWidth"],
        document.documentElement["offsetWidth"]
    );
    

    just replace Width for "Height" to get Height.

    0 讨论(0)
  • 2020-11-28 05:55
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;
    

    Check: this article for better explanation.

    0 讨论(0)
  • 2020-11-28 05:56

    Get document size without jQuery

    document.documentElement.clientWidth
    document.documentElement.clientHeight
    

    And use this if you need Screen size

    screen.width
    screen.height
    
    0 讨论(0)
  • 2020-11-28 05:56

    If you want to get the full width of the page, including overflow, use document.body.scrollWidth.

    0 讨论(0)
  • 2020-11-28 06:00

    This is a cross-browser solution:

    var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    
    0 讨论(0)
  • 2020-11-28 06:01

    How to find out the document width and height very easily?

    in HTML
    <span id="hidden_placer" style="position:absolute;right:0;bottom:0;visibility:hidden;"></span>

    in javascript

         var c=document.querySelector('#hidden_placer');
    
         var r=c.getBoundingClientRect();
         r.right=document width
         r.bottom=document height`
    

    You may update this on every window resize event, if needed.

    0 讨论(0)
提交回复
热议问题