Wednesday, February 27, 2008

Get the Height of the DIV in javascript

This will set the height of your div to the height of the browser window:
NOTE: 'divID' is the id name of the div in the html document.


window.onload = setDiv;

function setDiv() {
var wh = getWindowHeight(); // Window Height
var d = document.getElementById('divID') // Get div element
var dh = d.offsetHeight // div height
d.style.height = wh + 'px'; // Set div height to window height
}




function getWindowHeight() {
var windowHeight = 0;

if (typeof(window.innerHeight) == 'number')
windowHeight = window.innerHeight;

else {

if (document.documentElement && document.documentElement.clientHeight)
windowHeight = document.documentElement.clientHeight;

else {
if (document.body && document.body.clientHeight)
windowHeight = document.body.clientHeight; }; };

return windowHeight;
};

No comments:

Post a Comment