iframe自适应高度和宽度

本文介绍了一种解决在Chrome浏览器中iframe无法正确自适应其内容大小的问题的方法。通过检查不同浏览器内核对iframe的支持情况,实现了跨浏览器兼容性的解决方案。
<div>
  <iframe id="myframe" marginHeight=0 marginWidth=0 frameBorder=0 src="innerframe.html" onload="IFrameResize()"></iframe>
</div>

在Chrome中不行,其他浏览器都可以!

function IFrameResize()
{
	var iframe = document.getElementById("myframe"); 
	var iframeDocument = null;
	//safari和chrome都是webkit内核的浏览器,但是webkit可以,chrome不可以
	if (iframe.contentDocument)
	{ 
		//ie 8,ff,opera,safari
		iframeDocument = iframe.contentDocument;
	} 
	else if (iframe.contentWindow) 
	{ 
		// for IE, 6 and 7:
		iframeDocument = iframe.contentWindow.document;
	} 
	if (!!iframeDocument) {
		iframe.width=iframeDocument.documentElement.scrollWidth+"px";
		iframe.height=iframeDocument.documentElement.scrollHeight+"px";		
	} else {
		alert("this browser doesn't seem to support the iframe document object");
	} 

}

转载于:https://www.cnblogs.com/frouds/archive/2010/06/20/1761575.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值