Link to home
Start Free TrialLog in
Avatar of jeff0191
jeff0191

asked on

resizing an iFrame depending on cross-domain content

Hi.

I would like to resize an iFrame (height) depending on content (from another domain).  

I know it's possible as the website blogasty.com is doing it (just have a look at this URL:

http://blogasty.com/billet/359813-discussion-autour-de-l-avenir-des-digglike-sont-ils-appeles-a-mourir-ou-simplement-post

The bottom is an iFrame and the height is adjusting to the content height.

How he's doing it?

Thanks
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Where do you see it is resizing?

It has a width of 100% if that is what you mean

<iframe width="100%" height="1000px" frameborder="0" style="background-color: rgb(255, 255, 255); height: 566px;" src="..." id="globpage"></iframe>


Try the snippet:

sg
<iframe id="my_iframe" src="MY_IFRAME_URL" width="100%" scrolling="no" onload="checkHeight();"></iframe>

<script type="text/javascript">

   function checkHeight() {
      h = document.getElementById('my_iframe').contentDocument.body.scrollHeight;
      document.getElementById('my_iframe').style.height = h;
   }

</script>

Open in new window

I am pretty sure that will give you an access denied in most browsers
Yes - if the iframe url is external to your site, in which case you're stuffed. (soz - just saw that in the question - url from another domain).

I'm not sure there is a way to do it using an iframe...

sg
Avatar of jeff0191
jeff0191

ASKER

If you go on the home page and click on any article, you'll see that the iFrame is effectively resizing depending on content.  And this content is on an external website.  

Also, when I looked in the js file (site.js) there's a function adapt() where I can see:

$('globpage').style.height = (document.viewport.getHeight()-marginsize)+'px';

Not sure what this doing but this function is called in the body onload event.  
Cool

I searched for viewport and found prototype calls and this (scroll down for the comment)
https://support.zendesk.com/entries/56734-uber-iframe-widgets
That's a cool script mplungjan, but that's not doing what I want (it does an iFrame act like a lightbox but not resizing depending on iFrame content).  But i'll definitely bookmark this page as it could be cool to use it some time :)
so globpage is the ID of the iFrame
document.viewport must be the main document and not the iframe document.
So it is as far as I can see NOT resizing to the iframe content but resizing the iframe to the document containing the iframe
ASKER CERTIFIED SOLUTION
Avatar of jeff0191
jeff0191

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial