23

我有一个 iframe,您可以在以下链接中看到;-

http://one2onecars.com

iframe 是屏幕中央的在线预订。我遇到的问题是,虽然 iframe 的高度在页面加载时还可以,但我需要它以某种方式在页面内容调整时自动调整高度。例如,如果我在在线预订中进行邮政编码搜索,它会创建一个下拉菜单,然后使“下一步”按钮不可见。

我需要发生的是,当在线预订的内容发生变化时,iframe 会自动调整到 iframe 的新高度(动态),因为它没有加载任何其他页面。

我使用 jquery 尝试了几个不同的脚本来尝试解决这个问题,但它们似乎都只在页面首次加载时自动调整 iframe 的高度,而不是随着 iframe 内容的变化。

这甚至可能吗?

我目前拥有的代码目前具有设定的高度:-

        <div id="main-online-booking">

            <iframe id="main-online-frame" class="booking-dimensions" src="http://www.marandy.com/one2oneob/login-guest.php" scrolling="no" frameborder="0"></iframe>

        </div>

#main-online-booking {
    height: 488px;
    border-bottom: 6px #939393 solid;
    border-left: 6px #939393 solid;
    border-right: 6px #939393 solid;
    z-index: 4;
    background-color: #fff;
}


.booking-dimensions {
    width: 620px;
    height: 488px;
}

如果有人可以帮助我,我将不胜感激!

4

4 回答 4

31

设置间隔

只要 (由于浏览器技术的进步而更正,请参阅David Bradshaw 的回答使用 iframe 实现此目的的向后兼容方法是自己使用setInterval并密切关注 iframe 的内容。当它改变它的高度时,你会更新 iframe 的大小。不幸的是,没有这样的事件可以让您轻松聆听。

一个基本示例,这仅在大小已更改的 iframe 内容是主页面流的一部分时才有效。如果元素是浮动的或定位的,那么您必须专门针对它们来查找高度变化。

jQuery(function($){
  var lastHeight = 0, curHeight = 0, $frame = $('iframe:eq(0)');
  setInterval(function(){
    curHeight = $frame.contents().find('body').height();
    if ( curHeight != lastHeight ) {
      $frame.css('height', (lastHeight = curHeight) + 'px' );
    }
  },500);
});

显然,根据您的需要,您可以修改此代码的透视图,以便它从 iframe 自身运行,而不是期望成为主页的一部分。

跨域问题

您会发现的问题是,由于浏览器的安全性,如果 iframe 与主页位于不同的主机上,它不会让您访问 iframe 的内容,因此实际上您无能为力,除非您有添加方法出现在 iframe 中的 html 的任何脚本。

阿贾克斯

其他一些人建议尝试通过 AJAX 使用第三方服务,除非该服务支持这种方法,否则你不太可能让它工作——特别是如果它是很可能需要的预订服务通过 https/ssl 操作。

看起来您可以完全控制 iframe 内容,您可以选择完整的选项,使用 JSONP 的 AJAX 将是一个选项。但是,警告一句话。如果您的预订系统是多步骤的,那么您需要确保您有一个设计良好的用户界面——可能还有一些历史/片段管理代码——如果您要走 AJAX 路线。这一切都是因为您永远无法判断用户何时决定在浏览器中向前或向后导航(iframe 会在合理范围内自动处理)。设计良好的 UI 可能会妨碍用户这样做。

跨域通信

如果您可以控制双方(听起来像您这样做),您还可以使用跨域通信选项window.postMessage- 请参阅此处了解更多信息https://developer.mozilla.org/en-US/docs/DOM/window。留言

于 2013-01-15T09:17:03.390 回答
26

现代浏览器和部分 IE8 具有一些新功能,使这项任务比以前更容易。

留言

postMessage API 提供了一种在 iFrame 与其父级之间进行通信的简单方法。

要将消息发送到父页面,您可以按如下方式调用它。

parent.postMessage('Hello parent','http://origin-domain.com');

在另一个方向,我们可以使用以下代码将消息发送到 iFrame。

var iframe = document.querySelector('iframe');
iframe.contentWindow.postMessage('Hello my child', 'http://remote-domain.com:8080');

要接收消息,请为消息事件创建事件侦听器。

function receiveMessage(event)
{
  if (event.origin !== "http://remote-domain.com:8080")
    return;

  console.log(event.data);
}

if ('addEventListener' in window){
    window.addEventListener('message', receiveMessage, false);
} else if ('attachEvent' in window){ //IE
    window.attachEvent('onmessage', receiveMessage);

这些示例使用 origin 属性来限制消息发送到哪里并检查它来自哪里。可以指定*允许发送到任何域,并且在某些情况下您可能希望接受来自任何域的消息。但是,如果您这样做,您需要考虑安全隐患并对传入消息实施您自己的检查,以确保它包含您所期望的内容。在这种情况下,iframe 可以将其高度发布到“*”,因为我们可能有多个父域。但是,检查传入消息是否来自 iFrame 是个好主意。

function isMessageFromIFrame(event,iframe){
    var
        origin  = event.origin,
        src     = iframe.src;

    if ((''+origin !== 'null') && (origin !== src.substr(0,origin.length))) {
        throw new Error(
            'Unexpect message received from: ' + origin +
            ' for ' + iframe.id + '. Message was: ' + event.data  
        );
    }

    return true;
}

突变观察者

更现代的浏览器的另一个进步是MutationObserver,它允许您观察 DOM 中的变化;因此,现在可以检测可能影响 iFrame 大小的更改,而无需不断地使用 setInterval 进行轮询。

function createMutationObserver(){
    var
        target = document.querySelector('body'),

        config = {
            attributes            : true,
            attributeOldValue     : false,
            characterData         : true,
            characterDataOldValue : false,
            childList             : true,
            subtree               : true
        },

        observer = new MutationObserver(function(mutations) {
            parent.postMessage('[iframeResize]'+document.body.offsetHeight,'*');
        });

    log('Setup MutationObserver');
    observer.observe(target, config);
}

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

if (MutationObserver){
    createMutationObserver();
}

计算出准确的高度

获得 iFrame 的准确高度并不像应有的那么简单,因为您可以选择六种不同的属性来检查它们,但它们都没有给出始终正确的答案。我想出的最好的解决方案是只要你不使用 CSS 来溢出 body 标签,这个函数就可以工作。

function getIFrameHeight(){
    function getComputedBodyStyle(prop) {
        return parseInt(
            document.defaultView.getComputedStyle(document.body, null),
            10
        );
    }

    return document.body.offsetHeight +
        getComputedBodyStyle('marginTop') +
        getComputedBodyStyle('marginBottom');
}

这是 IE9 版本,对于很长的 IE8 版本,请参阅此答案

如果您确实溢出了正文并且您无法修复您的代码来阻止它,那么使用offsetHeight或的scrollHeight属性document.documentElement是您的最佳选择。两者都有优点和缺点,最好只是测试两者,看看哪个适合你。

其他问题

其他需要考虑的事情包括,页面上有多个 iFrame,CSS :Checkbox 和 :Hover 事件导致页面调整大小,避免在 iFrame 的 body 和 html 标签中使用 height auto,最后调整窗口大小。

IFrame 调整器库

我将所有这些都包含在一个简单的无依赖库中,它还提供了一些此处未讨论的额外功能。

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

这适用于 IE8+。

于 2014-04-10T15:08:09.280 回答
7

我写了这个脚本,它非常适合我。随意使用它!

function ResizeIframeFromParent(id) {
    if (jQuery('#'+id).length > 0) {
        var window = document.getElementById(id).contentWindow;
        var prevheight = jQuery('#'+id).attr('height');
        var newheight = Math.max( window.document.body.scrollHeight, window.document.body.offsetHeight, window.document.documentElement.clientHeight, window.document.documentElement.scrollHeight, window.document.documentElement.offsetHeight );
        if (newheight != prevheight && newheight > 0) {
            jQuery('#'+id).attr('height', newheight);
            console.log("Adjusting iframe height for "+id+": " +prevheight+"px => "+newheight+"px");
        }
    }
}

您可以在循环中调用该函数:

<script>
jQuery(document).ready(function() {
    // Try to change the iframe size every 2 seconds
    setInterval(function() {
        ResizeIframeFromParent('iframeid');
    }, 2000);
});
</script>
于 2013-10-11T06:52:36.750 回答
0

使用这个脚本:

$(document).ready(function () {
  // Set specific variable to represent all iframe tags.
  var iFrames = document.getElementsByTagName('iframe');

  // Resize heights.
  function iResize() {
    // Iterate through all iframes in the page.
    for (var i = 0, j = iFrames.length; i < j; i++) {
    // Set inline style to equal the body height of the iframed content.
      iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
    }
  }

 // Check if browser is Safari or Opera.
 if ($.browser.safari || $.browser.opera) {
    // Start timer when loaded.
    $('iframe').load(function () {
    setTimeout(iResize, 0);
    });

   // Safari and Opera need a kick-start.
   for (var i = 0, j = iFrames.length; i < j; i++) {
     var iSource = iFrames[i].src;
     iFrames[i].src = '';
     iFrames[i].src = iSource;
   }
 } else {
    // For other good browsers.
    $('iframe').load(function () {
    // Set inline style to equal the body height of the iframed content.
    this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
    });
  }
});

注意:在网络服务器上使用它。

于 2013-01-15T09:13:00.207 回答