Javascript 调整WebView中iFrame的大小
我试图使用iframes在一个Javascript 调整WebView中iFrame的大小,javascript,android,html,webview,Javascript,Android,Html,Webview,我试图使用iframes在一个WebViewhtml中显示多个html文件 我在html文件中使用以下脚本来调整当前html中iFrame的高度 <body onload="resizeFrames()" link="#bb7722" vlink="#bb7722" alink="#bb7722" > <script type="text/javascript"> // Firefox worked fine. Internet Explor
WebView
html中显示多个html文件
我在html文件中使用以下脚本来调整当前html中iFrame的高度
<body onload="resizeFrames()" link="#bb7722" vlink="#bb7722" alink="#bb7722" >
<script type="text/javascript">
// Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
function resizeFrames() {
var all_IFrames = document.getElementsByTagName('iframe');
i = 0;
while (frame = all_IFrames.item(i++)) {
resizeFrame(frame);
}
}
function resizeFrame(f) {
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}
</script>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="b.html"></iframe>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="c.html"></iframe>
我按如下方式加载html文件:
webView.loadUrl("file:///android_asset/a.html");
问题是,调整大小的iFrame有时太大,有时太小
我用安卓2.2在galaxy s上测试了它,所有的帧都变小了(最后被切掉),而在另一台安卓4.0.4的设备上,所有的帧都变大了(增加了空白)
如果我删除webSettings.setJavaScriptEnabled(true)代码>galaxy s中的问题消失了,但在第二台设备中,帧根本不会调整大小
完整html代码:
<!DOCTYPE html>
<html>
<head>
<style>
body{
color: #aa6611;
font-size:20px;
}
.link {
font-weight: bold;
color: #bb7722;
}
</style>
</head>
<body onload="resizeFrames()" link="#bb7722" vlink="#bb7722" alink="#bb7722" >
<script type="text/javascript">
// Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
function resizeFrames() {
var all_IFrames = document.getElementsByTagName('iframe');
i = 0;
while (frame = all_IFrames.item(i++)) {
resizeFrame(frame);
}
}
function resizeFrame(f) {
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}
</script>
<a name="top"></a>
<font size = "6" color="#ffaa55">Guide</font>
<br />
<br />
<a href="#mainscreen" class="link" >Main screen</a> <br />
<a href="#inventory" class="link">Inventory</a> <br />
<a href="#store" class="link">Store</a> <br />
<a href="#market" class="link">Market</a> <br />
<a href="#creatureattackdialog" class="link">Creature Attack Dialog</a> <br />
<a href="#userattackdialog" class="link">User Attack Dialog</a> <br />
<a href="#miniattackdialog" class="link">Mini Attack Dialog</a> <br />
<a href="#profile" class="link">Profile</a> <br />
<a href="#creatureinfo" class="link">Creature info</a> <br />
<a href="#userinfo" class="link">User info</a> <br />
<a href="#iteminfo" class="link">Item info</a> <br />
<a href="#lastattackers" class="link">Last Attackers</a> <br />
<a href="#achievementshistory" class="link">Achievements History</a> <br />
<a href="#achievementsDialog" class="link">Achievement info</a> <br />
<a href="#options" class="link">Options</a> <br />
<br />
<p>
<a name="mainscreen"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="mainscreen.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="inventory"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="inventory.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="store"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="store.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="market"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="market.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="creatureattackdialog"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="attackDialogCreature.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="userattackdialog"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="attackDialogUser.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="miniattackdialog"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="miniattackdialog.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="profile"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="profile.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="creatureinfo"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="creatureInfo.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="userinfo"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="userInfo.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="iteminfo"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="itemInfo.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="lastattackers"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="lastattackers.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="achievementshistory"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="acheivementhistory.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="achievementsDialog"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="acheivementDialog.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
<p>
<a name="options"></a>
<iframe width="100%" scrolling="no" frameborder=0 border=0 src="optionsDialog.html"></iframe>
</p>
<a href="#top" class="link">Back</a> <br />
</body>
</html>
身体{
颜色:#aa6611;
字体大小:20px;
}
.链接{
字体大小:粗体;
颜色:#bb7722;
}
//Firefox运行良好。由于框架边框的原因,Internet Explorer显示滚动条
函数resizeFrames(){
var all_IFrames=document.getElementsByTagName('iframe');
i=0;
while(frame=all_IFrames.item(i++)){
调整框架(框架)的大小;
}
}
函数大小调整框架(f){
f、 style.height=f.contentWindow.document.body.scrollHeight+“px”;
}
您需要为iframe标记中的onload处理程序设置侦听器,每个侦听器一个,并在加载iframe而不是主体时调整每个侦听器的大小
var iframe = document.getElementById('yourIF');
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
iframe.style.height = (iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight) + "px";
您需要为iframe标记中的onload处理程序设置侦听器,每个侦听器一个,并在加载iframe而不是主体时调整每个侦听器的大小
var iframe = document.getElementById('yourIF');
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
iframe.style.height = (iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight) + "px";