iframe automatic height adjustment using HTML5 cross domain web messaging

Yesterday I was working on a site that needed an iframe embedded in the page whose content came from a completely different domain. I try to steer clear of iframes at all costs normally so forgive me if what I’m about to say is pretty obvious.

I didn’t want the content in the iframe to appear as though it was in an iframe, so obviously I stripped off all the default browser styles…well, the border. I wanted the iframe to be as high as the content it was containing so that no scroll bars would appear.

So what I did was just give the iframe a height, however when the user navigated to a different page within the iframe, the height I had previously set was too big or too small.

I instantly thought “right, JavaScript will sort this out for me!” and proceeded to quickly code up a function that would inspect the scrollHeight of the content in the iframe and adjust the iframe height accordingly.

No! Said cross domain policy. I had momentarily forgotten all about that. Since the iframe content came from a different domain, I wasn’t allowed to access the document object of the iframe (or vice versa).

Here is how not to do it

Firefox says “Error: Permission denied to access property ‘document’” and other browsers similar.

I started searching for some kind of workaround.  Of course there isn’t really one…except I did find an interesting hack for Firefox. Which is – an iframe document can alter the url fragment of the parent window’s location object! ha madness.

The idea is that the document in the iframe alters the url fragment to read:

http://freestyle-developments.co.uk/blog/?p=150#138

Where 138 is the height of the iframe document (obviously you could be a bit more fancy and send other parameters and name them e.g #height:138 but for simplicity I’ve just set the value). The parent then reads this value and sets the iframe height accordingly:

var iFrameHeight = parseInt(location.hash.replace('#', ''));

So…yeah, that’s pretty interesting, but not really a solution. Check out the example implementation here.

Anyway, what to do? HTML5 web messaging to the rescue! Sweet! HTML5 web messaging is cross domain messaging done right. Not only does the messenger choose who should receive messages, but the recipient also chooses who to receive messages from.

The idea is that after the iframe document has loaded, it sends a message to the parent window telling it what it’s scrollHeight is. The parent listens for messages, and sets the height of the iframe when it receives a message.

A couple of things to note:

  1. There has to be communication between the two documents. i.e. the document on the external domain needs to actively send this message. Which can be a complete show stopper if you have no control over the document on the external domain
  2. This isn’t going to work on IE < 9. I tested the implementation linked below is working on FireFox 4 (Beta 7), Safari 5.0.2, Chrome 8, Opera 10.63 and Internet Explorer 9 Platform Preview 7. Which is a pretty good spread to be honest

Check out the example HTML5 web messaging implementation here.

24,080 thoughts on “iframe automatic height adjustment using HTML5 cross domain web messaging

  1. Hi,

    I have a public facing SharePoint 2010 site.

    Scenario: We are using cross scripting to show external sites information in our, site. We have pages with a Page Viewer web part ad CEWP. Height and scrolling issue of Page Viewer web part is resolved using cross scripting.we followed the below blog to resolve. and also your blog. in both the case its resolved

    http://blog.johnmckerrell.com/2006/10/22/resizing-iframes-across-domains/

    i want to make 100% sure that no search enginer like Google will mark our public facing SPSite as phishing site due to this cross site scripting??

    If you anybody want me to elaborate my question please let me know.

    Regards

    Guru

  2. Yes alan. you can view the source view of the blog.. same scripts i have copied and tried with my blog. i m not getting whats the problem with IE9 and firefox. in firefox its gives a wrong hieght, in IE9 it wont work at all. have followed something wrong or maybe that blog has internal few more iframes?
    what is the work around if i want to set the hieght of a single iframe with id.
    need guidance and help
    regards
    Guru

  3. Hi i tried to use your code, saved your page as a test my server

    http://myoldmac.net/MacFinder_share/iframecode3test.html

    i have not changed any code – now it does not display the message and does not resize. How do i get it to run ?

    I tried to include my page within the iframe but it does not resize.

    Because on your page it works, what do i wrong ?

    I try to get a code to implement this page on other pages:

    http://myoldmac.net/MacFinder_share/

    Thre problem is that it includes linklists with different heights and i need dynamic resizing. Thank you for any help!