看看这个
function iframeAutoFit()
{
try
{
if(window!=parent)
{
var a = parent.document.getElementsByTagName("IFRAME");
for(var i=0; i<a.length; i++) //
{
if(a[i].contentWindow==window)
{
var h = document.body.scrollHeight;
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
a[i].style.height = h;
}
}
}
}
catch (ex)
{
alert("脚本无法跨域操作!");
}
}
if(document.attachEvent) window.attachEvent("onload", iframeAutoFit);
else window.addEventListener('load', iframeAutoFit, false);
function iframeAutoFit()
{
try
{
if(window!=parent)
{
var a = parent.document.getElementsByTagName("IFRAME");
for(var i=0; i<a.length; i++) //
{
if(a[i].contentWindow==window)
{
var h = document.body.scrollHeight;
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
a[i].style.height = h;
}
}
}
}
catch (ex)
{
alert("脚本无法跨域操作!");
}
}
if(document.attachEvent) window.attachEvent("onload", iframeAutoFit);
else window.addEventListener('load', iframeAutoFit, false);
不过还是谢谢了。我看看能不能在你这基础上改改。
不过因此我发现了另一个郁闷的问题,昨天晚上我也是用document.body.scrollHeight取的,怎么值就不对呢?今天早上看了你这代码,我又把昨天晚上的重试了下,值又是对的了,狂倒。。
但FF里如何根据这个用脚本来重设本IFrame的高呢?用adverse(King)的.style.height是不行了,没作用。
继续讨论。
我也继续实验去。
if (o)
{
o.height = document.body.scrollHeight;
o.width = document.body.scrollWidth;
}
直接这样呢?
<table>
<tr><td><div id="leftMenu"><!-- 左边的菜单 --></div></td>
<td id="frameContainer" ><iframe scrolling="no" frameborder="no" name="main"></td></tr>
</table>设想是让iframe里面的页面去改变父窗口的iframe的高和宽。</p><p>以下是第一次尝试:function dyjustifyframe()
{
var o =window.parent.document.getElementsByName("main")[0];
o.height= document.body.scrollHeight;
o.width = document.body.scrollWidth;
}把function dyjustifyframe()放在iframe中嵌套的子页面里,让子页面的body onLoad="dyjustifyframe()"。但是iframe并没有height和width属性,所以上面不能这样写。于是找到第二个方法。用修改 iframe 上级的table的大小来间接改变其大小。先给iframe加上style="width:100%;height:100%"
使其随着外面table的大小改变而改变。于是把 dyjustifyframe()改成:function dyjustifyframe()
{
var o=window.parent.document.getElementById("frameContainer");
if (o)
{
o.height = document.body.scrollHeight;
o.width = document.body.scrollWidth;
}
}
上面的代码在ie中没问题,但在ff中调试时发现子页面的长度超出屏幕的话scrollHeight就只能变大不能变小,所以如果从一个长页面转到一个短页面的话父页面的长度不会改变。所以还需要修改一下。function dyjustifyframe()
{
var o= window.parent.document.getElementById("frameContainer");
if (o)
{
if (navigator.appName == "Netscape")
{
o.height = document.body.offsetHeight + 40;
o.width = document.body.offsetWidth + 20;
}
else
{
o.height = document.body.scrollHeight;
o.width = document.body.scrollWidth;
}
}
}要确保显示正常,还需要保证 frameContainer和leftMenu外面的td的大小没被设定,此外body的height必须是默认的auto,如果body是100%就会使代码失效。
我这没ff,试不了
我自己已经成功搞定了。
不过这才是FF和IE条件下的。
等下我下个其它常用的浏览器看看,行不行。
等都搞定了再发出来大家参考参考。
把我的代码贴出来先。function changeIframeHeight() //自动改变 Iframe 的高度,加在Iframe页面的Onload
{
if (top != self)
try
{
var h = document.body.scrollHeight;
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
var hpx = h+'px';
window.parent.document.getElementsByTagName('iframe')[0].style.height = hpx;//我的页面中只一个嵌入
}
catch(e)
{alert("脚本无法跨域操作!");}
}
另外,由于我是用Div+CSS来布局的,所以要在iframe标签中加入以下代码,以防在FF中由于父标签的高不够而造成部分内容无法显示:
<div id="Content"><iframe src="profile.html" id="mainFrame" name="mainFrame" width="1000" frameborder="0" scrolling="auto" height="440" onload="changp(this);"></iframe></div>changp()函数如下:function changp(c){
c.parentNode.style.height=c.style.height;
}
欢迎有其它浏览器和朋友测试,提出问题,再讨论解决问题。
用documentElement.scrollHeight;
用documentElement没有误差。。Iframe自适应高度。。
window.parent.document.getElementById(window.name).height = document.documentElement.scrollHeight;
草人前辈好久不见的说。。
WC1.asp
<!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>高度自己适应</title>
</head>
<body>
<iframe id="wc" name="wc" src="wc2.asp"></iframe>
</body>
</html>
WC2.asp<!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>高度自己适应</title>
<style type="text/css">
body {
margin:0px;
height:100%;
max-height:100%;
}
</style>
<script type="text/javascript">
window.onload = function () {
window.parent.document.getElementById(window.name).height = document.documentElement.scrollHeight;
}
</script>
</head>
<body>
<hr />
</body>
</html>
还有,是用Div+CSS布局的。
另外,大家发言最好是调试成功的,否则会浪费大家的时间。我也没有时间一个个地试你们贴的代码。
欢迎到我的Blog上讨论,来者有分。呵呵。
地址:http://www.biglee.cn/Blog/article.asp?id=6刚开没几天,大家来带点人气,来点彩头。HoHo~~~
刚才试了下你的代码,在单独这两页里是能执行,有效果,但,我把你这代码用到我的网页里,就不行了。还不清楚是哪的原因,现在没时间研究了。
反正我有我的解决方法了。有时间了再来研究。
等研究完了再结帖。
不要用body
用documentElement.scrollHeight;----------------------------------
用事实说话,经过实验,在IE下,documentElement.scrollHeight取得的值始终比body.scrollHeight取得的一致;而在FF下,当第一次转入页面时,documentElement比Body的值少(有时相等),少多少,要看Div+CSS里的CSS定义,没有一个定的数(具体有何规律,待有时间再研究,但我初步认为是容器div高的定义有关),但当点击页面的链接来改变IFrame里的页面时,如果新加载页面的documentElement.scrollHeight比上个页面的小,则取得的值会是上个页面的值。附我的实验代码:主页面代码:<!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" xml:lang="zh-CN" lang="zh-CN">
<head>
<title>Copmany</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/basic.css" rel="stylesheet" type="text/css">
<base target="mainFrame" />
<script language="javascript">
function changp(c){
//alert('p'+c.style.height);
c.parentNode.style.height=c.style.height;
}
<style type="text/css">
html {overflow-y:scroll;}
body {overflow-x:hidden;}
#Content {}
</style>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div id="Top">
<div class="Logo"><img src="images/Logo.gif" alt="" width="196" height="79" border="0" usemap="#Map">
<map name="Map" id="Map"><area shape="rect" coords="15,59,176,78" href="" onmouseover="showMenu(1)" onclick=" return false;"/></map></div>
<div class="ADNav">
<div class="AD">
<div class="AD1"><img src="images/Top_01.gif" width="739" height="59" alt=""></div>
<div class="AD2"><img src="images/Top_02.gif" width="63" height="59" alt=""></div>
</div>
<div class="Nav"><ul>
<li><a href="products.asp" class="navs" style="padding-left:9px;">Products</a></li>
<li><a href="Events.asp" class="navs" >Press</a></li>
</ul>
</div>
</div>
</div>
<div id="Content">
<iframe src="profile.asp" id="mainFrame" name="mainFrame" width="1000" frameborder="0" scrolling="auto" height="440" onload="changp(this);"></iframe>
</div><div id="Btm">
<div class="logo2"><img src="images/Logo2.gif" width="90" height="23" alt="" /></div>
</div>
</body>
</html>
IFrame嵌入页面包含的js:
function changeIframeHeight() //自动改变 Iframe 的高度
{
if (top != self)
try
{
var h = document.body.scrollHeight;//documentElement
var h1=document.documentElement.scrollHeight;
alert('C:body:'+h+',de:'+h1)
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
var hpx = h1+'px';
window.parent.document.getElementsByTagName('iframe')[0].style.height = hpx;
}
catch(e)
{alert("脚本无法跨域操作!");}
}
if(document.attachEvent) window.attachEvent("onload", changeIframeHeight);
else window.addEventListener('load', changeIframeHeight, false);
我这人比较喜欢弄个明明白白的。
大家继续发展意见。
还是那句话,不要只是贴一段不知道哪来的代码,要经过亲自实践,确认没问题,如果没有时间,请注明未经验证。
另外注意一点,是标准的Div+CSS布局(我感觉问题就在这),不是表格的堆砌。
document.frames[name].document.documentElement.scrollHeight
main.htm:<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<meta name='author' content='F.R.Huang(meizz梅花雪)//www.meizz.com' />
<title>iframe自适应加载的页面高度</title>
</head>
<body>
<div><iframe src="child.htm"></iframe></div>
</body>
</html>child.htm:<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<meta name='author' content='F.R.Huang(meizz梅花雪)//www.meizz.com' />
<title>iframe 自适应其加载的网页(多浏览器兼容)</title>
<script type="text/javascript">
<!--
function iframeAutoFit()
{
try
{
if(window!=parent)
{
var a = parent.document.getElementsByTagName("IFRAME");
for(var i=0; i<a.length; i++) //author:meizz
{
if(a[i].contentWindow==window)
{
var h1=0, h2=0;
a[i].parentNode.style.height = a[i].offsetHeight +"px";
a[i].style.height = "10px";
if(document.documentElement&&document.documentElement.scrollHeight)
{
h1=document.documentElement.scrollHeight;
}
if(document.body) h2=document.body.scrollHeight; var h=Math.max(h1, h2);
if(document.all) {h += 4;}
if(window.opera) {h += 1;}
a[i].style.height = a[i].parentNode.style.height = h +"px";
}
}
}
}
catch (ex){}
}
if(window.attachEvent)
{
window.attachEvent("onload", iframeAutoFit);
//window.attachEvent("onresize", iframeAutoFit);
}
else if(window.addEventListener)
{
window.addEventListener('load', iframeAutoFit, false);
//window.addEventListener('resize', iframeAutoFit, false);
}
//-->
</script>
</head>
<body>
<table border="1" width="200" style="height: 400px; background-color: yellow">
<tr>
<td>iframe 自适应其加载的网页(多浏览器兼容,支持XHTML)</td>
</tr>
</table>
</body>
</html> 很多人反应在IE7里使用它会死机,那是因为在自适应高度时触发了 window.onresize 事件,而这个事件又去调用这个调整 <iframe> 高度的函数,产生了死循环调用。
我正在用我写的那个方法啊。。
注意WC2.asp那个过度的DTD不可以去掉,如果还有问题。。把你的预嵌套页的CODE放上来研究研究就是=WC2.asp的这个页面的CODE。。
WC1.asp<iframe id="wc" name="wc" src="wc2.asp" frameborder="0"></iframe>WC2.asp<!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>高度自己适应</title>
<script type="text/javascript">
window.onload = function () {
window.parent.document.getElementById(window.name).height = document.documentElement.scrollHeight;
}
</script>
</head>
<body>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<hr />
</body>
</html>
这个就是我完成的。
目前我暂时没发现问题了。也欢迎各位测试,提出问题。