Monday 13 July 2009

Dynamic resizing of iFrame, pros and cons

If you are a web developer you have probably encountered iFrame resizing requirements.
Because of legacy code, your portal framework or some other reason you have to use an iFrame and because of the fancy layout from the designer you are required to introduce an iFrame that fills some section of the page that adjusts itself depending on the content that goes into the iFrame, and of course you do not want extra scroll bars appearing on this iFrame and you do not want a vertical scrollbars except from on the outer right side window edge when the page is long.

So how do you go about solving this problem? There is tons of sample code that shows what you can do but you will find few articles summing up pros and cons of alternative solutions.

Basically there are two solutions. I will explain them shortly and present a list of pros and cons. Examples of the two alternatives, I am sure you can find your self. jQuery can be used for both solutions.

Alternative 1) Resize iFrame from within inner frame
Description: With javascript on both the outer page (the one hosting the iFrame) and inner page, iFrame is resized after the inner page has checked its own height and sends this value to its parent. In order to allow cross frame scripting you may need to set the document.domain property on both pages.

Pros:
  • We get a scrollbar on the outer most right side (browser window) when the page is long. Same as on all other type of long pages.
  • Script only needed on long pages.
Cons:
  • Might require changes to browser security settings or modification of trusted sites list.
  • When the inner iFrame page has dynamic content (uses ajax/dhtml) that expands the initial page, multiple scrollbars will appear unless you attach your javascript to these dhtml functions.
  • Requires that you have control over pages that are to be hosted within the iFrame. javascript must be included on each page (or a shared include/template file).
  • Requires setting document.domain property using javascript which might cause future unexpected problems because of dependencies?.
  • Both hosted page and parent page need to have the same domain names (not the same subdomain) using the same protocol (http/https).

Alternative 2) Resize iFrame to fit page from outside
Description: Using javascript, iFrame is automatically resized to fit outer window on window load and resize event.

Pros:
  • Approach is guarantied to work (if JS is enabled). This means less risk and less chance of bugs.
  • Less complex solution and therefore more likely to work over time as you or your IT department do browser upgrades or make changes to browser settings.
  • Can be used to show external resources not under your control.
Cons:
  • The scrollbar will not span the entire height of the window. Only from the position of the top navigation menu or so and down to the window bottom.
  • While resizing the window there will be some flickering and scrollbars might seem sluggish.
  • iFrame must span entire width of window, this might put some restrictions on layout design.
  • Script to resize iFrame always have to be triggered on page load (and resize) and might contribute to slower rendering on very slow machines.

9 comments:

  1. I dont know if it solves your purposed problem, but you can do this without the use of Javascript. The solution used by Google Images ex. http://images.google.com/imgres?imgurl=http://fabulousblueporcupine.files.wordpress.com/2008/05/puppy1.jpg&imgrefurl=http://jessewhite.wordpress.com/2008/09/24/my-fellow-americans-its-time-for-the-mother-of-all-endorsements/&usg=__h8n9WQtx-AzX69PMT_eJ1uwe3GY=&h=367&w=491&sz=28&hl=no&start=1&sig2=yXMMNu3zZT167ju8lXy8Cg&tbnid=otOFWIwMG06xyM:&tbnh=97&tbnw=130&prev=/images%3Fq%3Dpuppy%26gbv%3D2%26hl%3Dno%26sa%3DG&ei=yNxsSoOHEtSqmQfn7tyOBA

    The solution is as somthing like the following:

    1. HTML element with height:100%;overflow:hidden
    2. BODY with overflow:hidden;height:100%
    3. TABLE with width="100%" height="100%" that has two rows, first row has height="1%" and has your non-iframe content, in Google Images they show a thumbnail of the image here. The second row contains the IFRAME with frameborder="0" scrolling="auto" and width:100%;height:100%

    I may have forgotten something, but thats about it.

    ReplyDelete
  2. Thank you for that. Using tables might be a good enough solution for many but for corporate sites or large public websites you probably want to have compliant markup and that means not using tables for layout.

    I guess, in Google's case, their image search page is so simple that nobody minds it not conforming.

    ReplyDelete
  3. i've wrote a post about the auto-resize iframe problem:

    http://www.amirharel.com/2009/07/28/implementing-iframe-toolbar/

    i show there how its being used to implement iframe toolbars.

    ReplyDelete
  4. Thank you Amir for an informative post about how to implement iFrame toolbars. The iFrame toolbar technique presented gives a nice summary of how to implement what I refer to as alternative 2 (Host page JS-based iFrame resizing)

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. You should not really use iframe's if you wan't compliant web pages; when XHTML only Transitional DOCTYPE allows iframe's. When it comes to using table in layouts being bad, then this is a common misunderstanding if you are referring to WCAG 1.0, the specification actually says "Do not use tables for layout unless the table makes sense when linearized", so make sure the layout table contents is linearized and you are good ;o)

    ReplyDelete
  7. Well Inge :) I totally agree iFrames should not be used now that we have Ajax.

    Anyway, tables are part of the HTML 4.01 spec and they are perfectly valid for presenting tabular content. E.g. A table of customers with name, country, phone numbers etc for columns. This all comes down to html semantics. If you are using tables to make an iFrame display in a particular way, that is not the proper use of tables and not very semantic in html/wcag terms. Tables are for presenting tabular content, it's as easy as that.

    ReplyDelete
  8. I'm not sure what you mean with "semantics in html/wcag terms", as I understand the WCAG specification it is pretty straight forward and obvious that tables can be used for layouts; WCAG speaks of two different kinds of table usage, "data tables" and "layout tables",

    The WCAG guidlines at:
    http://www.w3.org/TR/WCAG10/#gl-table-markup

    says the following:

    "Tables should be used to mark up truly tabular information ("data tables"). Content developers should avoid using them to lay out pages ("layout tables")."

    , this is where the many web developers gets the misunderstanding that "layout tables" are not allowed in WCAG 1.0, which is far from the truth, because a couple of lines further down one can read the following:

    "5.3 Do not use tables for layout unless the table makes sense when linearized."

    So what does "linarized" actually mean? Its so that tools like synthesized speech read the text in the layout table in the correct order so the user of such tools are not confused.

    Hope this clear things up :)

    ReplyDelete
  9. I think we agree.
    From a pure WCAG point of view, yes it is valid but from a semantic CSS/HTML point of view it's bad.

    The acronyms got a bit mixed up, it should be "Semantic in html/css terms". Check out http://bit.ly/to7h2 to read about what I mean by that.

    ReplyDelete

Spammers will be hunted down and punished.