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).
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:
- 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
- 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.
Nice blog here! Also your site loads up fast! What host are you using?
Can I get your affiliate link to your host? I wish my web site
loaded up as fast as yours lol
I am not certain where you’re getting your information, but good topic.
I must spend a while studying much more or understanding more.
Thank you for excellent information I used to be on the lookout
for this information for my mission.
https://t.me/s/TopGo1WIN
Helpful information. Fortunate me I found your site unintentionally,
and I’m surprised why this coincidence didn’t happened earlier!
I bookmarked it.
Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the website is also very good.
ремонт кофемашин bork https://remont-kofemashin1.ru
ремонт бытовых швейных машин ремонт швейных машин
You made some good points there. I checked on the web to find out more
about the issue and found most people will go along with your views on this web
site.
Heya i am for the first time here. I found this board and I
to find It really helpful & it helped me out a lot. I am hoping to provide something back and
help others such as you aided me.
Hello, I enjoy reading all of your article. I like to write a little comment to support you.
I visited multiple websites however the audio
feature for audio songs current at this website is genuinely wonderful.
hey there and thank you for your information – I’ve definitely picked
up anything new from right here. I did however expertise a few technical points using
this site, since I experienced to reload the site lots of times previous to
I could get it to load properly. I had been wondering if your web hosting is OK?
Not that I am complaining, but sluggish loading instances
times will very frequently affect your placement in google and can damage your high quality score if advertising and marketing with Adwords.
Well I am adding this RSS to my e-mail and can look out
for a lot more of your respective intriguing content.
Make sure you update this again very soon.
Greetings from California! I’m bored at work so I decided to check out your blog on my iphone during lunch break.
I enjoy the knowledge you provide here and can’t
wait to take a look when I get home. I’m shocked at
how fast your blog loaded on my mobile .. I’m not even using WIFI, just 3G ..
Anyhow, fantastic site!
Hi there! This post couldn’t be written much better!
Reading through this article reminds me of my previous roommate!
He constantly kept preaching about this. I’ll send this article to him.
Pretty sure he’s going to have a good read. I appreciate you for sharing!
Every weekend i used to pay a visit this site, as i wish for enjoyment, for the reason that this this web page conations genuinely
nice funny material too.
Hi there all, here every person is sharing these kinds of knowledge, thus it’s pleasant to read this website, and I used to visit this web site all the time.
I was suggested this web site by my cousin. I am not sure whether this post
is written by him as no one else know such detailed about my problem.
You’re amazing! Thanks!
Awesome! Its actually amazing piece of writing, I have got much clear idea concerning from this post.
Thankfulness to my father who stated to me on the
topic of this blog, this website is in fact remarkable.