• Home
Pictures Archives Travel RSS

iFrame dynamic resizing revisited

05 June 2008 | Coding, JavaScript | Suneth Mendis

Its time to revisit the burning question once again!

“Is there a reliable way to dynamically resize the iFrame height according to its contents?”

Well according to my previous post I thought I nailed it! But I soon found out it wasn’t all that reliable! :( So it wasn’t the silver bullet I was hoping for. 

So as a part of “E2″ development, my mate Rob and I started on a journey to conquer this once and for all! Our findings were some what disturbing!

First of all, there ISN’T a silver bullet that solves the problem for all the browsers. The basic problem is that iFrames (along with applets) are handled differently on browsers as compared to other elements. In fact iframes are not handled by browsers at all, but by the OS. So the solution will never be the same for all the browsers. So heres what we found. 

IE 6/7

The solution is a nasty CSS hack. Until yesterday I have not heard about this trick. The trick is to have an expression like this in your CSS.

.iframe { height: expression(document.documentElement.clientHeight + "px"); }

IE being a special browser, it detects this expression and resize your iFrame accordingly. But there is a down side to this. Having expressions in your CSS affects your site’s performance. But having battered by this issue for days and nights, I am willing to go down this path for now!

FireFox 2 and 3 / Safari 3

Yes, they both behave the same way! So trick here is to wrap your iframe with a DIV tag and assign an  absolute positioning to the DIV. 

<div id="iframe_wrap"><iframe scrolling="auto" id="iframe" name="iframe" class="iframe" frameborder="0" src="source_page.html" width="100%" height="100%"></iframe></div>

And then apply styles to the DIV tag like below. 

#iframe_wrap {
position:absolute;
top: 60px;
left: 0;
right: 0;
bottom: 0;
}

This nice trick will get ride of the “double scrollbar” phenomenon as well. 

So there you go. The job is done!!! (At least for now) Special thanks to Rob for doing most of the “googling”!

Popularity: 78% [?]

8 Responses to “iFrame dynamic resizing revisited”

  1. Suneth Mendis » Iframe dynamic resizing… SOLVED!
    June 5th, 2008 at 12:08 am

    […] UPDATE: Check out my next post for a better solution.  […]

  2. Nick
    June 25th, 2008 at 6:14 pm

    Is that it - or do I need to install any Javascript? (At the moment it isn’t working for me.)

  3. Suneth Mendis
    June 25th, 2008 at 6:24 pm

    No Javascript required. It is purely CSS driven. Would you mind posting a simple use case of your example so I can have a look? (Just sample HTML will do)

  4. Nick
    June 26th, 2008 at 12:38 pm

    Notes:

    1. Using Joomla installation http://axeandcrom.com
    2. Using ComicCMS installation http://axeandcrom.com/comic

    Need:

    1. Comic to be dynamically iFramed into Joomla install

    Attempt One:
    I tried putting the code into a Joomla module and published it from there.

    1. with CSS in a seperate doc
    2. with CSS inline

    Neither worked, so I thought it might be a Joomla thing.

    Attempt Two:
    Put code into the index.php of the live template.

    1 & 2 same as above.

    Code Attempt 01:

    Code Attempt 02:
    http://axeandcrom.com/comic/

    Code Attempt 03:
    http://axeandcrom.com/comic/index.php

    CSS Placements:
    I tried external style sheet, header, and inline.

    It would be awesome if you could assist.
    Thx,

  5. Nick
    June 26th, 2008 at 12:41 pm

    A poop - I’m Sorry, I don’t know how to flag code so that it isn’t rendered…

  6. Julks
    August 17th, 2008 at 6:22 am

    Hi Suneth,

    How to put the code in one meaning that will work in both IE and Firefox in just one code.

  7. Suneth Mendis
    August 17th, 2008 at 1:50 pm

    Combining both is a BAD thing. As I said CSS expressions are really bad. So use it only when it absolutely necessary. By doing something like,

    you can isolate IE and put in the hack.

  8. Concentratedmedia Blog » Blog Archive » Creating Dynamic iframes for IE 6 & 7, Safari 3, Firefox 2 & 3
    September 2nd, 2008 at 9:27 pm

    […] this but the best solution is to work with a little trick in CSS,I found a great tip on this web site,   The best solution, that works for IE 6&7 | Safari 3 | and Firefox 2 & 3:Is the […]

Leave a Reply

«Using jQuery with Prototype - Sweet!!!

Auto coloring of child nodes using Prototype»

About Me

One life, Live it!

My name is Suneth Mendis. I'm a 29 year old Software Engineer. I live in Brisbane Australia and working for a very exciting company called Ephox.

  • Recent Posts

    • Ignorance is your loss
    • Auto coloring of child nodes using Prototype
    • iFrame dynamic resizing revisited
    • Using jQuery with Prototype - Sweet!!!
    • “Fun” with Prototype and IE7 and Tables
    • What serious lack of sleep can do to a developer…
    • iSurprise
    • Offline blog editor
    • What!?
    • Iframe dynamic resizing… SOLVED!
    • more...
  • Categories

    • Agile
    • Blogroll
    • Coding
    • Gadgets
    • Hobbies
    • Ideas
    • JavaScript
    • Life
    • Mac
    • Prototype
    • Travel
    • Work
    • WTF
  • Links

    • Planet Ephox

Copyright © Suneth Mendis 2008 · Designed by Suneth Mendis · Powered by WordPress