PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » jQuery » Resize iFrame to Fit Content (Same Domain Only)

Resize iFrame to Fit Content (Same Domain Only)

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.

Subscribe to The Thread

  1. Callum says:

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

    • Maku says:

      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 says:

    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. Nancy says:

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

  5. eJames says:

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

    -cheers

  6. dhana says:

    Hi,

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

  7. Melisande says:

    excellent!
    thanks :D

  8. skyrail says:

    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.

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~