Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.body.clientHeight not working? (JavaScript)

Tags:

javascript

document.body.clientHeight not returning correct values (in below code return 0 onload and 20 onresize )

<html>
<head>
<title>Untitled</title>
<script type="text/javascript" >
function height1()
{
    document.getElementById("viewheight").innerHTML = document.body.clientHeight;
}
</script>
</head>

<body onload="height1();" onresize="height1();">
<span id="viewheight"></span>
</body>
</html>

Any Comments..... plz help!

like image 890
abhishek-23 Avatar asked Dec 22 '12 08:12

abhishek-23


1 Answers

Try it with document.getElementById("viewheight").innerHTML = window.innerHeight;

The behaviour of your script is totally correct. At the start the height of your body is really zero (body height is the height of the document). Because there's nothing shown, the height results with zero. After resizing your printed "0" spans over 20px, so the result will stay at 20px.

If you want to get the maximum height of the window or the body you could use Math.max( window.innerHeight, document.body.clientHeight )

like image 68
bukart Avatar answered Sep 19 '22 03:09

bukart