JavaScript实现Iframe自适应高度:同域与跨域方案详解

作者:半夏

时间:2026-10-28

浏览:154

本文详细讲解如何利用JavaScript实现Iframe的高度自适应,涵盖同域名与不同域名两种场景。同域下通过onload事件直接获取内容高度;跨域时则需借助中介页面,利用URL hash传递高度信息以规避跨域限制,确保页面布局紧凑且无需滚动条。

文章目录
1. 同域名下Iframe自适应高度的处理

同域名下Iframe自适应高度处理

代码实现如下:

 
<iframe onload="Javascript:SetIFrameHeight(this)" src="../Home/b" id="win" name="win" width="100%" height="1"> </iframe>

此处示例基于Asp.Net MVC框架,src属性配置为路由结构。


相关JavaScript代码如下:

 
<script type="text/javascript">
function SetIFrameHeight(obj) {
var win = obj;
if (document.getElementById) {
if (win && !window.opera) {
if (win.contentDocument && win.contentDocument.body.offsetHeight)
win.height = win.contentDocument.body.offsetHeight;
else if (win.Document && win.Document.body.scrollHeight)
win.height = win.Document.body.scrollHeight;
} 
} 
} 
</script>

核心逻辑在于:当Iframe嵌套的页面加载完成后,触发onload事件,获取嵌套网页的实际高度,并将其赋值给Iframe元素的高度属性。


2. 跨域时Iframe高度自适应

跨域时Iframe高度自适应方案

代码示例如下:

 
当主页面与被嵌套的Iframe处于不同域名时,处理逻辑会更为复杂,主要目的是规避JavaScript的跨域访问限制。
实现原理涉及三个页面:主页面main.html、被嵌套页面iframe.html以及中介页面agent.html。假设main.html域名是http://www.ccvita.com,iframe.html域名是http://www.phpq.net。当用户浏览时,iframe.html中的JavaScript代码会将自身高度信息附加到iframeC的src地址中;随后,同域的agent.html读取该高度,并通过JavaScript动态设置main.html中Iframe的高度,从而达成自适应目标。
以下是Iframe主页面main.html的代码:
[code]
< !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><title>iframe主页面</title></head>
<body>
<div style="border:1px solid #ccc;padding:10px;">
<iframe id="frame_content" name="frame_content" src="iframe.html" width="100%" height="0" scrolling="no" frameborder="0"></iframe>
</div>
<br />尾部<br />
</body>
</html>

接下来是被Iframe嵌套页面iframe.html的代码:


代码如下:

 
<!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><title>被iframe嵌套页面</title></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 /><br /><br /><br /><br /><br /><br /><br />
文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<iframe id="iframeC" name="iframeC" src="" width="0" height="0" style="display:none;" ></iframe>
<script type="text/javascript">
function sethash(){
hashH = document.documentElement.scrollHeight;
urlC = "agent.html";
document.getElementByIdx("iframeC").src=urlC+"#"+hashH;
} 
window.onload=sethash;
</script>
</body>
</html>

最后是Iframe中介页面agent.html的代码:


代码如下:

 
< !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><title>iframe中介页面</title></head>
<body>
<script>
function pseth() {
var iObj = parent.parent.document.getElementByIdx('frame_content');
iObjH = parent.parent.frames["frame_content"].frames["iframeC"].location.hash;
iObj.style.height = iObjH.split("#")[1]+"px";
} 
pseth();
</script>
</body>
</html>

代码中路径表示可能存在不完整的情况。


关键约束:main.html与agent.html必须位于同一域名下。


而iframe.html则位于另一个不同的域名中。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章