Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.body.clientHeight returns invalid result

While searching for a method to find out the window height, i came to know that 'document.body.clientHeight' is useful. I tried it in different situations everything failed. Failed means that it returns same value in all window heights.

Finally I removed the DOCTYPE declaration

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

and everything went ok. Now it return exact result.

Even html5 DOCTYPE

<!DOCTYPE html>

return invalid result.

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

What is the reason for that? Is there any other restrictions while using document.body? Is there any other things(variables, tags, methods, usage, etc) under the same scenario?

Thank you.

like image 341
Vijeesh Avatar asked Jan 01 '26 20:01

Vijeesh


1 Answers

Try this

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";
like image 90
user2804021 Avatar answered Jan 03 '26 11:01

user2804021



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!