iframe的高度一直是让人头疼的问题。今天因为有个项目需要用到。再把这个问题找了一遍,但是还是发现了FF不兼容的错误(提示错误: document.frames is undefined)!
在这里我把这个问题彻底解决了。希望大家可以借鉴!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js实现的非跨域的iframe高度自适应(完美兼容IE,FF,Opera)</title>
<style>
*{margin:0;padding:0;}
</style>
<script type="text/javascript">
function SetCwinHeight(obj)
{
var cwin=obj;
if (document.getElementById)
{
if (cwin && !window.opera)
{
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
cwin.height = cwin.contentDocument.body.offsetHeight; //FF NS
else if(cwin.Document && cwin.Document.body.scrollHeight)
cwin.height = cwin.Document.body.scrollHeight;//IE
}
else
{
if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)
cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera
}
}
}
</script>
</head>
<body>
bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />
bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />bdaf<br />
<iframe src="index.html" name="ifr4" id="ifr4" frameborder="0" height="100%" width="100%" onload ="SetCwinHeight(this)"></iframe>
</body>
</html>