是否有跨域iframe高度自动调整器有效?

J82*_*J82 95 iframe cross-domain

我尝试了一些解决方案,但没有成功.我想知道是否有一个解决方案,最好有一个易于遵循的教程.

Jan*_*kia 66

你有三种选择:

1.使用iFrame-resizer

这是一个简单的库,用于保持iFrames的内容大小.它使用PostMessage和MutationObserver API,以及IE8-10的后备.它还有内容页面的选项,以请求包含iFrame的特定大小,并且当您完成它时也可以关闭iFrame.

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

2.使用Easy XDM(PostMessage + Flash组合)

Easy XDM使用一系列技巧来实现许多浏览器中不同窗口之间的跨域通信,并且有一些示例可以将它用于iframe大小调整:

http://easyxdm.net/wp/2010/03/17/resize-iframe-based-on-content/

http://kinsey.no/blog/index.php/2010/02/19/resizing-iframes-using-easyxdm/

Easy XDM通过在现代浏览器上使用PostMessage和基于Flash的解决方案作为旧版浏览器的后备工作.

另请参阅Stackoverflow上的这个主题(还有其他人,这是一个常见问题).此外,Facebook似乎也采用了类似的方法.

3.通过服务器进行通信

另一个选择是将iframe高度发送到您的服务器,然后使用JSONP从父网页轮询该服务器(如果可能,使用长轮询).

  • **这些“替代方案”都不是该问题的实际解决方案。提问者想要设置他们无法控制的网站的跨域 iFrame 的 iFrame 高度。1. 需要服务器访问。2. 需要我认为很差的软件。Easy XDM 是 10 年前开发的。2019 年起的最新版本**需要闪存**。幸运的是,Flash 已经死了,没有人应该依赖它。3. 需要服务器访问。 (8认同)
  • iFrame-resizer需要访问托管iframe内容的服务器.因此,您只能将其用于您控制的域. (6认同)

小智 21

我得到了根据内容动态设置iframe高度的解决方案.这适用于跨域内容.要实现这一目标,需要遵循一些步骤.

  1. 假设您已在"abc.com/page"网页中添加了iframe

    <div> <iframe id="IframeId" src="http://xyz.pqr/contactpage" style="width:100%;" onload="setIframeHeight(this)"></iframe> </div>

  2. 接下来,你必须在网页"abc.com/page"下绑定windows"message"事件

window.addEventListener('message', function (event) {
//Here We have to check content of the message event  for safety purpose
//event data contains message sent from page added in iframe as shown in step 3
if (event.data.hasOwnProperty("FrameHeight")) {
        //Set height of the Iframe
        $("#IframeId").css("height", event.data.FrameHeight);        
    }
});
Run Code Online (Sandbox Code Playgroud)

  1. 在iframe这里添加的主页面"xyz.pqr/contactpage"你必须绑定windows"message"事件,其中所有消息都将从"abc.com/page"的父窗口接收

function setIframeHeight(ifrm) {
   var height = ifrm.contentWindow.postMessage("FrameHeight", "*");   
}
Run Code Online (Sandbox Code Playgroud)

最后,您的iframe高度将在iframe加载时设置.

快乐的编码!

  • 需要明确的是,步骤 3 意味着您需要&lt;b&gt;控制目标服务器&lt;/b&gt; (xyz.pqr) 来添加“FrameHeight”消息的侦听器。这不适用于您无法控制的目标服务器。 (4认同)
  • 做得很好.谢谢堆! (2认同)
  • 没有 jQuery 部分:`document.querySelector("#IframeId").style.height = event.data.FrameHeight;` (2认同)

Tro*_*hel 14

我做的是比较iframe scrollWidth直到它改变大小,同时我逐步设置IFrame高度.它对我来说很好.您可以将增量调整为所需的值.

   <script type="text/javascript">
    function AdjustIFrame(id) {
        var frame = document.getElementById(id);
        var maxW = frame.scrollWidth;
        var minW = maxW;
        var FrameH = 100; //IFrame starting height
        frame.style.height = FrameH + "px"

        while (minW == maxW) {
            FrameH = FrameH + 100; //Increment
            frame.style.height = FrameH + "px";
            minW = frame.scrollWidth;
        }
    }

   </script>


<iframe id="RefFrame" onload="AdjustIFrame('RefFrame');" class="RefFrame"
    src="http://www.YourUrl.com"></iframe>
Run Code Online (Sandbox Code Playgroud)

  • 你可能想在循环次数上添加一个上限,否则'while'可以退化为无限循环. (3认同)
  • 这导致我的页面崩溃。 (3认同)
  • 这个主意看起来很棒,但无法重新创建:http://codepen.io/anon/pen/BNKgZm (2认同)