Return to Snippet

Revision: 46329
at May 17, 2011 19:39 by dubogii

Initial Code
Normally you set and width and height for iframes. If the content inside is bigger, scrollbars have to suffice. The script below attempts to fix that by dynamically resizing the iframe to fit the content it loads.

<script type='text/javascript' src=''></script>
<script type='text/javascript'>


        var iFrames = $('iframe');

        function iResize() {

                for (var i = 0, j = iFrames.length; i < j; i++) {
                  iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';}

                if ($.browser.safari || $.browser.opera) { 

                       setTimeout(iResize, 0);

                   for (var i = 0, j = iFrames.length; i < j; i++) {
                                var iSource = iFrames[i].src;
                                iFrames[i].src = '';
                                iFrames[i].src = iSource;

                } else {
                   iFrames.load(function() {
              = this.contentWindow.document.body.offsetHeight + 'px';



Will resize an iframe like this:

<iframe src="content.html" class="iframe" scrolling="no" frameborder="0"></iframe>

Initial URL

Initial Description
Still problematic...

1. The source of the iframe content must reside on the same domain
2. if the content inside the iframe changes height, this won't adapt
3. I left Google Analytics code off the demo above, as when I added it in it seems to interfere and not resize the iframe, despite seemingly generating no errors.

Initial Title
Resize iFrame to Fit Content (Same Domain Only)

Initial Tags

Initial Language