Adjust width and height of iframe to fit with content in it

前端 未结 30 2329
旧时难觅i
旧时难觅i 2020-11-22 02:09

I need a solution for auto-adjusting the width and height of an iframe to barely fit its content. The point is that t

相关标签:
30条回答
  • 2020-11-22 02:47

    This is how I did it onload or when things change.

    parent.jQuery("#frame").height(document.body.scrollHeight+50);
    
    0 讨论(0)
  • 2020-11-22 02:49
    <script type="application/javascript">
    
    function resizeIFrameToFitContent( iFrame ) {
    
        iFrame.width  = iFrame.contentWindow.document.body.scrollWidth;
        iFrame.height = iFrame.contentWindow.document.body.scrollHeight;
    }
    
    window.addEventListener('DOMContentLoaded', function(e) {
    
        var iFrame = document.getElementById( 'iFrame1' );
        resizeIFrameToFitContent( iFrame );
    
        // or, to resize all iframes:
        var iframes = document.querySelectorAll("iframe");
        for( var i = 0; i < iframes.length; i++) {
            resizeIFrameToFitContent( iframes[i] );
        }
    } );
    
    </script>
    
    <iframe src="usagelogs/default.aspx" id="iFrame1"></iframe>
    
    0 讨论(0)
  • 2020-11-22 02:49

    I am using this code to autoadjust height of all iframes (with class autoHeight) when they loads on page. Tested and it works in IE, FF, Chrome, Safari and Opera.

    function doIframe() {
        var $iframes = $("iframe.autoHeight"); 
        $iframes.each(function() {
            var iframe = this;
            $(iframe).load(function() {
                setHeight(iframe);
            });
        });
    }
    
    function setHeight(e) {
      e.height = e.contentWindow.document.body.scrollHeight + 35;
    }
    
    $(window).load(function() {
        doIframe();
    });
    
    0 讨论(0)
  • 2020-11-22 02:49

    It is possible to make a "ghost-like" IFrame that acts like it was not there.

    See http://codecopy.wordpress.com/2013/02/22/ghost-iframe-crossdomain-iframe-resize/

    Basically you use the event system parent.postMessage(..) described in https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

    This works an all modern browsers!

    0 讨论(0)
  • 2020-11-22 02:50

    I slightly modified Garnaph's great solution above. It seemed like his solution modified the iframe size based upon the size right before the event. For my situation (email submission via an iframe) I needed the iframe height to change right after submission. For example show validation errors or "thank you" message after submission.

    I just eliminated the nested click() function and put it into my iframe html:

    <script type="text/javascript">
        jQuery(document).ready(function () {
            var frame = $('#IDofiframeInMainWindow', window.parent.document);
            var height = jQuery("#IDofContainerInsideiFrame").height();
            frame.height(height + 15);
        });
    </script>
    

    Worked for me, but not sure about cross browser functionality.

    0 讨论(0)
  • 2020-11-22 02:50

    In case someone getting to here: I had a problem with the solutions when I removed divs from the iframe - the iframe didnt got shorter.

    There is an Jquery plugin that does the job:

    http://www.jqueryscript.net/layout/jQuery-Plugin-For-Auto-Resizing-iFrame-iFrame-Resizer.html

    0 讨论(0)
提交回复
热议问题