Thursday, December 22, 2005

Javascript window.innerWidth vs. document.body.offsetWidth

var winW = parseInt("630"), winH = parseInt("460");
if (parseInt(navigator.appVersion)>3) {
if (navigator.appName=="Netscape") {
winW = window.innerWidth;
winH = window.innerHeight;
}
if (navigator.appName.indexOf("Microsoft")!=-1) {
winW = document.body.offsetWidth;
winH = document.body.offsetHeight;
}
}


The above code returns the width and height of the browser window regardless if it's Netscape or IE. If the browser is Netscape, we use innerWidth and innerHeight properties of the window object to return the width and height respectively of the browser window. If the browser is IE we use the offsetWidth and offsetHeight properties of the body object of the document object to return the width and height respectively of the browser window.

No comments: