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.

325 thoughts on “iframe automatic height adjustment using HTML5 cross domain web messaging

  1. Психотерапевт Белгород. chat-s-psikhologom-v-telegramme.ru 616 оценок

  2. Психотерапевт Челны. chat-s-psikhologom-v-telegramme.ru 160 оценок

  3. Б17 психологи. chat-s-psikhologom-v-telegramme.ru 962 оценок

  4. Как сложить бюстгальтер в чемодан Как сложить вещи в чемодан. 624 благодарностей

  5. Как сложить бюстгальтер в чемодан Как сложить вещи в чемодан в самолет 975 благодарностей

  6. Упаковать чемодан компактно Как складывать вещи в чемодан чтобы они не мялись 760 благодарностей

  7. Дизайн человека 20 46 52 Ворота Дизайн человека 35 Ворота Дизайн человека 57 Ворота Дизайн человека 20 Ворота Дизайн человека 51 Ворота Дизайн человека 61 Ворота Дизайн человека 58 Ворота Дизайн человека 5 Ворота Дизайн человека 14 Ворота Дизайн человека 59 Ворота Дизайн человека 44 Ворота Дизайн человека

  8. Дизайн человека 43 53 18 Ворота Дизайн человека 10 Ворота Дизайн человека 57 Ворота Дизайн человека 36 Ворота Дизайн человека 37 Ворота Дизайн человека 54 Ворота Дизайн человека 44 Ворота Дизайн человека 19 Ворота Дизайн человека 54 Ворота Дизайн человека 40 Ворота Дизайн человека 3 Ворота Дизайн человека

  9. Hi there! I know this is kinda off topic but I was
    wondering which blog platform are you using for this website?
    I’m getting fed up of WordPress because I’ve had issues with hackers
    and I’m looking at options for another platform.
    I would be fantastic if you could point me in the direction of a good platform.

  10. Дизайн человека 16 37 47 Ворота Дизайн человека 10 Ворота Дизайн человека 49 Ворота Дизайн человека 29 Ворота Дизайн человека 54 Ворота Дизайн человека 51 Ворота Дизайн человека 62 Ворота Дизайн человека 60 Ворота Дизайн человека 4 Ворота Дизайн человека 59 Ворота Дизайн человека 45 Ворота Дизайн человека

  11. Hi this is kinda of off topic but I was wanting to know if blogs use WYSIWYG
    editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise so
    I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

  12. For hottest information you have to go to see web and on internet I found this site as a finest site for most up-to-date updates.

  13. Wow, this paragraph is nice, my younger sister is analyzing these kinds of things, thus I am going
    to tell her.

  14. Fastidious response in return of this question with genuine arguments and telling the
    whole thing regarding that.

  15. Every weekend i used to go to see this website, for the
    reason that i wish for enjoyment, since this
    this web page conations genuinely good funny material too.

  16. Hello my friend! I wish to say that this article is
    amazing, great written and include almost all significant infos.
    I’d like to look more posts like this .

  17. Wow! This blog looks exactly like my old one!

    It’s on a completely different subject but it has pretty much the
    same page layout and design. Superb choice of colors!

  18. I like the helpful info you provide in your articles.
    I’ll bookmark your blog and check again here frequently. I’m
    quite sure I’ll learn lots of new stuff right here! Good luck
    for the next!

  19. Wow, fantastic blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your website is wonderful, let alone the content!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>