在处理iframe的高度自适应这个问题时,按照一般的写法,在父页面里的javascript应该是这样的:
function initIframe() {
var iframe = document.getElementById("mainframe");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight =iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height + "px";
}catch (ex){}
}
父页面里iframe的定义:
<iframe id="mainframe" name="mainframe" src="index.html" onload="initIframe();" width="100%" height="100%" frameborder="0" scrolling="no"></iframe> 在其它浏览器如IE系列、firefox、opera、safari等里都能正确运行,但在chrome5和chrome6里都取不到iframe的高度。
于是测试一下,
alert(iframe.contentWindow.document);
只有在chrome里弹出是undefined,其它浏览器都是object HTMLDocument,原来chrome不支持跨frame的变量的获取!于是乎,在iframe页面里调用window.parent.**在chrome里也是不允许的!
这样的话,在chrome里,按照一般的方法无法使iframe的高度自适应。那么,有没有其它的方法,能解决这个问题呢?本人在探索中,也期待大家能提出好的意见。
<div style="width:200px;float:left;">
<iframe id='t' scrolling="auto" style="height:100%;width:100%; "src="http://www.baidu.com"></iframe>
</div>
iframe.height = (子页面动态改变的高度);
在父页面里是通过一个定时器:
window.setInterval(initIframeHeight, 200);
来不断获取子页面动态改变的高度的。
现在的问题在于,只有chrome不能获取子页面的高度(或许chrome它认为这与安全有关),其它主要的浏览器都ok。
答案:E
f.scrolling = "auto";
}