我正在 Android 中开发一个网络应用程序,我使用 WebView 来播放一些页面。以下是我的html代码,我写了一个js函数来获取我的浏览器屏幕高度。
<html>
<head>
<script>
window.onload=function(){
alert(window.innerHeight);
}
</script>
</head>
<body style="background:black;">
</body>
</html>
我在我的 Defy 480*800 移动设备和 WebView 中测试了上面的代码,它只返回 0,有时返回其他数字。但在浏览器中,它工作正常。
所以我想知道是否有什么我应该知道的才能在 WebView 中获取可用的屏幕高度?
更新:2012.3.13
感谢 Demonick,我按照你的建议找到了 lazyload.js,我使用 setTimeout 进行轮询以获取窗口的高度。
希望对其他人有帮助。
<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>
最佳答案
尝试将 window.innerHeight
函数放在 document.onready
或更高版本中,因为高度可能尚未在 onload
上初始化还没有。
关于javascript - WebView中的js函数window.innerHeight只返回0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9613591/