跨域iframe调整器?

时间:2011-04-09 18:18:13

标签: iframe

我正在寻找一个好的跨域iframe大小调整脚本,根据其内容调整其高度。我也可以访问html / css以获取iframe的来源。那里有没有?

7 个答案:

答案 0 :(得分:34)

如果您的用户使用的是现代浏览器,则可以使用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>

答案 1 :(得分:14)

由于未能找到解决所有不同用例的解决方案,我最终编写了一个支持宽度和高度的简单js lib,在一个页面上调整内容和多个iframe的大小。

https://github.com/davidjbradshaw/iframe-resizer

答案 2 :(得分:1)

EasyXDM可以做到这一点:) This blog pos解释了它的要点

答案 3 :(得分:1)

此页面上的第一个脚本 - 在HTML5中使用postMessage的脚本 - 也适用于移动设备上的iframe - 通过调整内容的iframe大小 - 例如联合跨域 - 您可以轻松地在iphone或android中滚动用iframes无法实现的方式

答案 4 :(得分:0)

经过一番研究后,我最终使用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上进行了测试。 ;)

答案 5 :(得分:0)

以下代码为我工作:

var iframe = document.getElementById(id);  
iframe.height = iframe.contentDocument.body.scrollHeight;

在Opera 11,IE 8 9,FF 8,Chrome 16上测试

答案 6 :(得分:0)

我有一个完全不同的解决方案来跨域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');" height="5600"></iframe>
相关问题