跨域 iframe 调整大小?
Posted
技术标签:
【中文标题】跨域 iframe 调整大小?【英文标题】:cross-domain iframe resizer? 【发布时间】:2011-08-02 04:35:00 【问题描述】:我正在寻找一个很好的跨域 iframe 大小调整脚本,它可以根据其内容调整其高度。我也可以访问 iframe 源的 html/css。有吗?
【问题讨论】:
查看这个简单的解决方案:***.com/questions/7024574/… 是我还是所有这些答案都要求您将脚本添加到 external 页面? 【参考方案1】:如果您的用户使用现代浏览器,您可以使用postMessage in HTML5 轻松解决此问题。这是一个效果很好的快速解决方案:
iframe 页面:
<!DOCTYPE html>
<head>
</head>
<body onload="parent.postMessage(document.body.scrollHeight, 'http://target.domain.com');">
<h3>Got post?</h3>
<p>Lots of stuff here which will be inside the iframe.</p>
</body>
</html>
包含 iframe 的父页面(并且想知道它的高度):
<script type="text/javascript">
function resizeCrossDomainIframe(id, other_domain)
var iframe = document.getElementById(id);
window.addEventListener('message', function(event)
if (event.origin !== other_domain) return; // only accept messages from the specified domain
if (isNaN(event.data)) return; // only accept something which can be parsed as a number
var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
iframe.height = height + "px";
, false);
</script>
<iframe src='http://example.com/page_containing_iframe.html' id="my_iframe" onload="resizeCrossDomainIframe('my_iframe', 'http://example.com');">
</iframe>
【讨论】:
它对我有用;唯一的缺点是远程 iFramed 站点也需要编辑,如果不可编辑,这将不起作用 在 Chrome(版本 56.0.2924.87)中尝试过,但没有成功。不会出现任何错误,但也不会调整大小。【参考方案2】:由于未能找到处理所有不同用例的解决方案,我最终编写了一个简单的 js 库,它支持宽度和高度、调整内容大小和一个页面上的多个 iframe。
https://github.com/davidjbradshaw/iframe-resizer
【讨论】:
这个库非常令人印象深刻,似乎解决了我在其他解决方案中遇到的所有其他问题。谢谢你的工作。【参考方案3】:我有一个完全不同的跨域 iframe 调整大小的解决方案。它涉及获取您将放入 iframe 的目标页面的副本,将其写入本地,然后将该副本放入您的 iframe 并根据对框架内 dom 的相同域访问调整大小。
一个例子如下:
<?php
if(isset($_GET['html'])) $blogpagehtml = file_get_contents(urldecode($_GET['html']));
else $blogpagehtml = file_get_contents('http://r****d.wordpress.com/');
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($blogpagehtml);
libxml_use_internal_errors(false);
$anchors = $doc->getElementsByTagName("a");
foreach($anchors as $anchor)
$anchorlink=$anchor->getAttribute("href");
if(strpos($anchorlink,"http://r****d.wordpress")===false) $anchor->setAttribute("target","_top");
else $anchor->setAttribute("href","formatimportedblog.php?html=".urlencode($anchorlink));
$newblogpagehtml = $doc->saveHTML();
$token = rand(0,50);
file_put_contents('tempblog'.$token.'.html',$newblogpagehtml);
?>
<iframe id='iframe1' style='width:970px;margin:0 auto;' src='tempblog<?php echo $token; ?>.html' frameborder="0" scrolling="no" onLoad="autoResize('iframe1');" ></iframe>
【讨论】:
【参考方案4】:此页面上的第一个脚本 - 在 HTML5 中使用 postMessage 的脚本 - 也适用于移动设备上的 iframe - 通过将 iframe 调整为内容的大小 - 例如跨域联合 - 您可以轻松地在 iphone 或 android 中滚动否则 iframe 无法做到这一点
【讨论】:
【参考方案5】:以下代码对我有用:
var iframe = document.getElementById(id);
iframe.height = iframe.contentDocument.body.scrollHeight;
在 Opera 11、IE 8 9、FF 8、Chrome 16 上测试
【讨论】:
这仅在 iframe src 与父级位于同一域中时才有效。【参考方案6】:经过一番研究,我最终使用了 html5 的消息传递机制,该机制包含在 jQuery pluging 中,这使得它可以使用各种方法(其中一些在此线程中描述)与旧版浏览器兼容。
最终的解决方案很简单。
在主机(父)页面上:
// executes when a message is received from the iframe, to adjust
// the iframe's height
$.receiveMessage(
function( event )
$( 'my_iframe' ).css(
height: event.data
);
);
// Please note this function could also verify event.origin and other security-related checks.
在 iframe 页面上:
$(function()
// Sends a message to the parent window to tell it the height of the
// iframe's body
var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined);
$.postMessage(
$('body').outerHeight( true ) + 'px',
'*',
target
);
);
我已经在 XP 和 W7 上的 Chrome 13+、Firefox 3.6+、IE7、8 和 9 以及 OSX 和 W7 上的 safari 上进行了测试。 ;)
【讨论】:
【参考方案7】:EasyXDM 可以做到这一点:) This blog post 解释了它的要点
【讨论】:
我刚看了看,似乎超出了我的专业范围。 请参阅 easyxdm.net/wp/2010/03/17/resize-iframe-based-on-content 以获得解释 - 真的没有更简单的方法。以上是关于跨域 iframe 调整大小?的主要内容,如果未能解决你的问题,请参考以下文章