Document.body.clientHeight returns invalid result

When searching for a method for determining the height of a window, I found out that "document.body.clientHeight" is useful. I tried it in different situations, everything failed. Failed means that it returns the same value at all window heights.

Finally, I deleted the DOCTYPE ad

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

and everything went fine. Now it returns the exact result.

Even html5 DOCTYPE

<!DOCTYPE html>

returns an invalid result.

I tested it in different browsers (Chrome, Opera, Firefox and IE).

What is the reason for this? Are there any other restrictions when using document.body? Are there other things (variables, tags, methods, usage, etc.) for the same scenario?

Thank.

+3
source share
2 answers

document.getElementById("viewheight").innerHTML = window.innerHeight


   <!DOCTYPE html>,  puts the document into standards mode.

adding + "px" to the end of the size setting line
  document.getElementById("viewheight").innerHTML = document.body.clientHeight + "px";
+2

,

document.documentElement.clientHeight
+1

All Articles