I am developing a web app in Android, and I use a WebView to play some pages. The following is my html code and I write a js function to get the height of my browser screen height.
<html>
<head>
<script>
window.onload=function(){
alert(window.innerHeight);
}
</script>
</head>
<body style="background:black;">
</body>
</html>
I test the above code in my Defy 480*800 mobile device and in the WebView, it just returns 0 and sometimes other number. But in browser, it works right.
So I wonder if there is something I should know to get the available screen height in WebView?
update: 2012.3.13
Thanks Demonick, I follow your suggestion and find the lazyload.js,I use the setTimeout to make a poll to get the window’s height.
Hope it helps others.
<script>
//poll numbers
var pollCount=0;
//get the window Height
var pollWindowHeight=function(){
//poll the window innerHeight 10s
if(window.innerHeight==0){
pollCount+=1;
if(pollCount<200) {
setTimeout(pollWindowHeight,50);
}else{
//just stop polling or you can do some others
}
}else{
//use the window.innerHeight to do something...
}
}
document.addEventListener("DOMContentLoaded",pollWindowHeight, false);
</script>
Reference: https://github.com/rgrove/lazyload/
Try to put the
window.innerHeightfunction indocument.onreadyor later instead, because the height might have not been initialized ononloadyet.