129

How to get document height and width in pure i.e without using .
I know about $(document).height() and $(document).width(), but I want to do this in .

I meant page's height and width.

Barnee
  • 3,212
  • 8
  • 41
  • 53
Saritha
  • 1,947
  • 5
  • 18
  • 24

11 Answers11

143
var height = document.body.clientHeight;
var width = document.body.clientWidth;

Check: this article for better explanation.

Damb
  • 14,410
  • 6
  • 47
  • 49
  • 11
    This is not cross browser. You should have tested this before posting it. –  Mar 09 '14 at 20:47
  • 14
    @Iwazaru Thanks for pointing that out. Looks like this 4 years old reply isn't bulletproof :) – Damb Mar 30 '15 at 15:26
  • 4
    It seems [it is cross-browser](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight#Browser_compatibility). – MAChitgarha Jan 14 '19 at 20:31
  • If a website has a border it takes that off the width with this solution but not the jQuery solution. Unlikely to be a border on the body but worth knowing. `window.innerWidth` solves this – BritishSam Mar 11 '19 at 14:59
  • This returns the height of the browser window (if you adjust the height of the browser it changes), not the height of the content/webpage. – Radmation Nov 08 '19 at 20:36
62

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.

Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
Dan
  • 1,112
  • 9
  • 15
  • 1
    This should be the correct answer. clientWidth does not always have the right value. – Wildhammer Jul 17 '19 at 18:53
  • 1
    problem with clientWidth is it is viewable only. scrollWidth includes things leaking off the screen. at least, this is true for my Chrome experiments in 2019. – StayCool Nov 06 '19 at 11:49
  • @StayCool it seems that the most recent jQuery is still using this method for the document height/width calculation and it gives the same result as `$(document).width()`. Maybe you are after something like `$(window).width()` which in pure JS would be just `document.documentElement["clientWidth"]` on modern browsers. – Dan Nov 08 '19 at 16:08
45

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;
hamid
  • 852
  • 11
  • 27
26

You should use getBoundingClientRect as it usually works cross browser and gives you sub-pixel precision on the bounds rectangle.

elem.getBoundingClientRect()
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
13

Get document size without jQuery

document.documentElement.clientWidth
document.documentElement.clientHeight

And use this if you need Screen size

screen.width
screen.height
tamasviktor
  • 131
  • 1
  • 5
8

This should work for all browsers/devices:

function getActualWidth()
{
    var actualWidth = window.innerWidth ||
                      document.documentElement.clientWidth ||
                      document.body.clientWidth ||
                      document.body.offsetWidth;

    return actualWidth;
}
user937284
  • 2,454
  • 6
  • 25
  • 29
  • window.innerWidth is the only one that is also correct when the dev tools are open on Chrome – Alex Sep 13 '15 at 14:54
8

You can try also:

document.body.offsetHeight
document.body.offsetWidth
Mihai Frentiu
  • 1,602
  • 1
  • 10
  • 9
  • 1
    Be careful of using these in things like window resize events. they have to calculate the height & width, which takes a while. – Miles Jul 24 '14 at 19:48
4

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

Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
2

window is the whole browser's application window. document is the webpage shown that is actually loaded.

window.innerWidth and window.innerHeight will take scrollbars into account which may not be what you want.

document.documentElement is the full webpage without the top scrollbar. document.documentElement.clientWidth returns document width size without y scrollbar. document.documentElement.clientHeight returns document height size without x scrollbar.

user3798451
  • 51
  • 1
  • 1
  • 6
1

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.

Joshy Francis
  • 340
  • 7
  • 13
0

Use this:

var w = window.innerWidth;

var h = window.innerHeight;

It gets the browser window width and height. This has more info: https://www.w3schools.com/howto/howto_js_get_current_window.asp

<!DOCTYPE html>
<html>
<body>

<h2>Get the size of the current screen/window</h2>
<p>The example displays the browser window's height and width: (NOT including toolbars/scrollbars)

<p id="demo"></p>

<script>
var w = window.innerWidth;
var h = window.innerHeight;

var x = document.getElementById("demo");
x.innerHTML = "Code snippet width: " + w + ", height: " + h + ".";
</script>

</body>
</html>

Try it here: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_get_current_window

Best Codes
  • 11
  • 3