简体   繁体   English

在内容刷新或更新时调整iframe的大小

[英]Resize iframe on content refresh or update

I have an iframe that I would like to resize to match its contents whenever the contents auto refreshes (every few minutes) or when the user interacts with it. 我有一个iframe,只要内容自动刷新(每隔几分钟)或当用户与其互动时,我都希望调整其大小以匹配其内容。 Although the contents are in the same domain, it would be difficult for me to modify the contents. 尽管内容在同一个域中,但我很难修改内容。 Ideally, the iframe would just self adjust to the size of its contents. 理想情况下,iframe可以自我调整以适应其内容的大小。

Current code that resizes only once: 当前代码仅调整一次大小:

<iframe id="ganglia-frame" src="ganglia.url" width="100%" height="500%">
    blah not supported blah
</iframe>

<script language="Javascript">
    function setIframeHeight(iframe) {
        if (iframe) {
            var iframeWin = iframe.contentWindow ||
                    iframe.contentDocument.parentWindow;
            if (iframeWin.document.body) {
                iframe.height =
                        iframeWin.document.documentElement.scrollHeight ||
                        iframeWin.document.body.scrollHeight;
            }
        }
    }

    $(window).load(function () {
        setIframeHeight(document.getElementById('ganglia-frame'));
    });
</script>

Related question: Adjust width height of iframe to fit with content in it 相关问题: 调整iframe的宽度高度以适合其中的内容

What you need to do is to set a timer and check the iFrame size after a few moments. 您需要做的是设置计时器,过一会儿再检查iFrame的大小。 You may have to check several times as not all browsers return the correct size immediately. 您可能需要检查几次,因为并非所有浏览器都会立即返回正确的大小。

This method works only on same-domain iFrames. 此方法仅适用于相同域的iFrame。

Bind a function to the iFrame onload event and when it executes check the height of the iFrame. 将函数绑定到iFrame onload事件,并在执行时检查iFrame的高度。 If no height is found schedule another check in a few moments. 如果没有找到高度,请稍后再进行检查。

var iFrameSizeCount = 0;
var onloadFunction = function(event){
    var contentHeight = document.getElementById('iFrameId').contentWindow.document.body.offsetHeight;

    if(contentHeight == 0){
         // Schedule a recheck in a few moments
         iFrameSizeCount++; // we keep a count of how many times we loop just in case
         if(iFrameSizeCount  < 10){ // after a while we have to stop checking and call it a fail
             setTimeout(function(){ onloadFunction(event); }, 200);
             return false;
         }
         else {
              contentHeight  = 100; // eventually if the check fails, default to a fixed height. You could possibly turn scrolling to auto/yes here to give the iFrame scrollbars.
         }
     }
    contentHeight += 30; // add some extra padding (some browsers give a height that's slightly too short)

    document.getElementById('iFrameId').style.height = contentHeight + 'px';
}

Then bind the event to the onload event of your iFrame (however you want to): 然后将事件绑定到iFrame的onload事件(无论如何):

The "scrolling=auto" is useful, just in case the sizing fails at least they have scrollbars. “ scrolling = auto”很有用,以防大小调整失败,至少它们具有滚动条。 The onload event fires if the iFrame reloads, so is useful if they've clicked a link inside it. 如果iFrame重新加载,则会触发onload事件,因此如果他们单击了其中的链接,则很有用。

我对这个jQuery插件有好运-https: //github.com/davidjbradshaw/iframe-resizer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM