treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Resize iFrame to Fit Content (Same Domain Only)

Last updated on:

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='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
<script type='text/javascript'>
    
    $(function(){
    
        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) { 
        	
        	   iFrames.load(function(){
        	       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.style.height = this.contentWindow.document.body.offsetHeight + 'px';
        	   });
        	}
        
        });

</script>

Will resize an iframe like this:

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

 

View Demo

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.
View Comments

Comments

  1. Permalink to comment#

    Excellent, this is exactly what I was looking for :)

    • Maku
      Permalink to comment#

      does anyone experienced bug on IE? …IE didnt resize the iframe instead it rendered a fixed height for the iframe. Any fix yet? Thanks

  2. Thall

    I doesnt work in IE 7 or 8. Tested in IETester

  3. Hi Chris, I’m wondering if you know of any script that would adapt the height of the iframe if the content inside the iframe changed height. Thanks!

  4. I am real new at this. Where within the page code do I put your code?

  5. MAN! this is what i needed, i dont know why i didnt think of it, absolute lifesaver

    -cheers

  6. dhana
    Permalink to comment#

    Hi,

    above code working in ie7 fine.but its not working in ie8.could u please send the code for that.

  7. excellent!
    thanks :D

  8. skyrail
    Permalink to comment#

    tested with success in IE9 for windows.
    Set doctype transitional on top of html code.

    Still need some tweaks to adjust the page width I want.

  9. Soshian
    Permalink to comment#

    Hello !!!
    Your code not work in chrome explorer ( your code = other codes ).
    Please send true code for me.
    thanks.

  10. Permalink to comment#

    hello everyone. same question here. where should i put script above.

    thank you

  11. Permalink to comment#

    Thanks for the script! I added styles to the iframe for IE, FF & Chrome.

    Thanks Muz

  12. George
    Permalink to comment#

    Thanks for the script. I decided to used it for now. If it breaks for different browsers I’ll try and think or find something else. Meanwhile I also found this:
    http://www.iframehtml.com/iframe-scripts.html#resizeable-iframe

    Thought I’d share it here. Seems easy and quick to use, the demo page works fine but this solution didn’t agree with my code.

  13. Stas
    Permalink to comment#

    Do not work with IE 6:(
    Maybe I can set minimal height?

  14. Permalink to comment#

    I got around the cross domain issue (atleast for subdomains) with
    document.domain = “yourdomain.com”

  15. me
    Permalink to comment#

    Works like a charm. Thanks.

  16. Does anybody know how to do this when you embed a PDF in the iframe? I’m generating the PDF dynamically with a server-side script (ASP.NET MVC). The problem I’ve found is that, when the .load function, the PDF hasn’t been rendered to the iframe yet, so the “current” height is something like 150px (default maybe).

    Any ideas?

    (Great post by the way!)

  17. Try to add edit your code on if statement replace your code on this code, hope it works:

    if ($.browser.safari || $.browser.opera || $.browser.msie && $.browser.version==”6.0″) {

    iFrames.load(function(){
    setTimeout(iResize, 0);
    });

  18. tim
    Permalink to comment#

    The height DOES adapt to changing content in the iframe, in latest IE and Firefox!

  19. Thierry
    Permalink to comment#

    I am from Brasil and this tuto is show, show, show.
    excellent!
    thanks :D

  20. I didn’t think this was possible for some reason. I’m building a site right now with various tables loading from another site (sport scores). Being able to resize the iframe like this will make it look like part of the main site. Awesome!

  21. Permalink to comment#

    This works for me even on IE and it doesn’t require jQuery

    
    
  22. Christoph
    Permalink to comment#

    Hey, so is there maybe an update on this article? I have tried several versions and i still get problems with Safari for example. Is there really a workaround that works on all browsers IE7+?
    Cheers

  23. Shweta
    Permalink to comment#

    Hello,
    thanks for useful script.
    It works fine for file from same domain.
    But I want to show Google spreadsheet in iframe.
    and above script does not work for me.
    Please can you suggest solution.

  24. Halfist
    Permalink to comment#

    In Firebug I see next error:

    Error: Permission denied to access property ‘document’

    this.style.height = this.contentWindow.document.body.offsetHeight + ‘px’;

Leave a Comment

Use markdown or basic HTML and be nice.