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.
https://englishzoom.ru/
Thanks for the comprehensive read. Find more at windshield replacement options .
Quality content is the secret to attract the viewers to pay a
quick visit the web page, that’s what this site is providing.
I just couldn’t depart your web site prior
to suggesting that I extremely loved the usual info a person provide to your visitors?
Is gonna be again continuously to check out new posts
Списался с данным магазом пару месяцев назад беру клады по москве ребята работают отлично сервис на высоте Последний раз даже клад сделали минут за 15 всего первый раз такое в других магазах приходилось часов по 5 ждать свое клада Рега тоже отличная беру JV-90 очень мощная штука Спасибо ребятам за качественный товар и сервис продолжайте в том же духе купить онлайн мефедрон экстази бошки Ну так ведь и не делаем не чотка . всё сразу .
Shoutout to an incredible %%lawyer near London ON%% who genuinely cares about their clients’ well-being—thank you for everything! real estate lawyer london ontario
I always emailed this weblog post page to all my contacts, because if like to read
it then my contacts will too.
Thanks for the detailed guidance. More at Powiększanie ust .
“Encouraging everyone here: prioritize ensuring everything functions properly before relying heavily on devices.” share.google
Thanks for your marvelous posting! I genuinely enjoyed reading it, you might be a
great author. I will ensure that I bookmark your blog and will eventually come back
at some point. I want to encourage yourself to continue your great posts, have a
nice weekend!
This was a fantastic resource. Check out Opiniones taxi Arzúa for more.
Curious whether anyone familiarized themselves with nutrition guidelines alongside training regimens followed religiously lately – would love diving deeper into aspects revolving dietary choices shared previously across various posts referenced directly Private Boxing Trainer Vancouver
When seeking advice after a car accident, consulting with experienced auto accident attorney can provide valuable insights and assistance. It’s essential to have qualified professionals by your side during challenging times like these!
BrightSpark Essentials Hub – Smooth browsing with a clean, accessible layout.
Looking for dedicated legal support in Portland? These personal injury lawyers are top-notch! Their experience and commitment to their clients make them stand out in the legal field. Motorcycle accident lawyer
http://ruptur.com/
fashionpick – Browsing is seamless, and the site feels organized and responsive.
For quick Hamilton roof repair after wind damage, I recommend getting a free estimate via Hamilton roofing professionals .
I found this very helpful. For additional info, visit https://www.homify.com/professionals/9936784/majewski-plumbing .
Helpful suggestions! For more, visit abogado penalista Vigo .
This was highly educational. More at casas rurales para grupos Segovia .
Are you preparing a family reunion? Think about utilizing portable toilet rental to ensure everyone remains comfortable!
https://batery-game.com/ios
If you’re planning an outside occasion in Fresno, consider using porta potty rental for your portable toilet rental needs. They provide terrific service!
You will not regret choosing porta potties near me for your mobile toilet leasing in St. Petersburg– they are great!
Feeling grateful having chosen ####Anykeyword#### as contractor responsible installing new slate tiles over existing structure previously ! Cork roof construction
Well explained. Discover more at trusted mobile auto glass Charlotte .
My experience with Floor Coverings International St Augustine was superb! They offer fantastic carpet options for those living in Nocatee. hardwood flooring
Same here—comfort and care matter. Looking for the Best Optometrist in Chino CA and comparing choices on optometrist near me .
This is the roughly reasonable tips vendors need. For residential architect functions in NYC, we suggest Residential architect NYC .
The healthcare professionals in Scottsdale are truly dedicated to improving lives through effective pain management; find out more at Regenerative Medicine Doctor !
Appreciate the detailed insights. For more, visit vet Bryceville FL .
Noble Ridge Select – Easy-to-use site, stylish items, and the package arrived quickly.
Anyone compare aluminum vs steel eavestroughs in Kitchener’s climate? Leaning aluminum with proper leaf guards. Might go through roof replacement estimates for install.
You’re exact that DIY builders have limits; for custom recommendations, strive reliable products and services at web design essex (Essex elegant).
I can’t state enough good things about Integral Builders! Their attention to detail is wonderful. Builders Surbiton
Trust me when I say that Summit Services is the number one choice for electricians near me in Bluffton. Electrician