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.

19,987 thoughts on “iframe automatic height adjustment using HTML5 cross domain web messaging

  1. Great insights on the importance of proactive fire safety! One thing I’d add is the value of routine inspections and NFPA-compliant maintenance to catch issues before they escalate—especially for sprinklers, alarms security cameras near me
home security systems near me
security systems near me
fire protection services near me
business security systems near me
fire protection near me
access control companies near me
access control near me
access control systems near me
business security near me

  2. “Visiting sites where famous figures rest encourages reflection upon their contributions shaping today’s world whilst celebrating shared humanity across time periods & cultures alike; learn more about notable gravesites documented within resources emergency gravestone company

  3. カジノの入出金、私も過去に何度も失敗しました。特に出金は「〇〇時間以内に処理」と謳っていても、実際は3〜5営業日かかることが多いです。私の体験では、例えばエコペイズは手数料がほぼ無料(銀行送金でだいたい30〜50円程度)で、処理時間も平均2営業日なのでオススメ。ただし、本人確認書類の提出は必須で、これを省くと結局遅れるので注意。PayPayやApple https://wiki-cable.win/index.php/%E3%82%AB%E3%82%B8%E3%83%8E_%E6%B1%BA%E6%B8%88%E6%96%B9%E6%B3%95_%E9%81%B8%E3%81%B3%E6%96%B9_%E5%88%9D%E5%BF%83%E8%80%85%E5%90%91%E3%81%91

  4. カジノ体験についてですが、特にボーナス周りのトラブルは初心者にとって厄介ですよね。例えば、ボーナスの出金条件が曖昧だったり、賭け金の計算方法が分かりづらいことが多いので、**必ずスクリーンショットで規約を保存**しておくのがおすすめです。これをしておくと、後でトラブルになったときに証拠として使えます。

    入出金に関しては、最近はスティックペイやエコペイズのような決済サービスを使うと、日本円での入出金がスムーズにできて助かっています。暗号資産でもいいですが、円換算で考える習慣をつけると資金管理がラクになりま https://wiki-view.win/index.php/%E3%81%AA%E3%81%9C%E3%82%AA%E3%83%B3%E3%83%A9%E3%82%A4%E3%83%B3%E3%82%AB%E3%82%B8%E3%83%8E%E3%81%AE%E8%B3%AD%E3%81%91%E6%9D%A1%E4%BB%B6%E3%81%AF40%E5%80%8D%E3%82%82%E3%81%82%E3%82%8B%E3%81%AE%E3%81%8B%EF%BC%9F%E3%81%9D%E3%81%AE%E4%BB%95%E7%B5%84%E3%81%BF%E3%81%A8%E7%8F%BE%E5%AE%9F%E7%9A%84%E3%81%AA%E3%83%9C%E3%83%BC%E3%83%8A%E3%82%B9%E5%87%BA%E9%87%91%E6%9D%A1%E4%BB%B6%E3%82%92%E5%BE%B9%E5%BA%95%E8%A7%A3%E8%AA%AC

  5. Great breakdown of the Best shade structure options for backyards. I recently compared pergolas vs. sail shades and found sail shades far easier to install for renters. If anyone wants a quick guide with pros/cons by budget, check out miami pergolas .

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>