views:

188

answers:

2

I use javascript to expand an Iframe to the size of the document it loads to remove any vertical scrollbar, like so:

function resizeIframes() {
    $('iframe').load(function()
        {
            this.style.height = (this.contentWindow.document.body.offsetHeight + 40) + 'px';
        }
    );
}

Which works well enough for my uses. But now I need to load the Iframe with a website from another server (actually another subdomain, instead of "www.mydomain.com" it's "services.mydomain.com") and according to Firebug I'm not allowed to read properties from other domains via Javascript. I'm guessing some kind of sandbox problem?!

Is there any way to circumvent this or at least get some kind of info about the size of the content? I even would be willing to check if there is a scrollbar and continually grow the iframe until it's gone, but unlike the window object an iframe object does not seem to have a .scrollbars property.

+1  A: 

I am sorry if you will find this an incomplete reply (it is) I just dont have code samples with me. I'll scetch the main concept and you will have to do the details..

You have to use some tricks to do that since XSS security does not allow frames from one site to access frames from another site:

site1:page1: call iframe (site2:page1) on site two . Let's assume the iframe ID property is "myframe".

site2:page1: run javascript (in body.onload) to calculate the size of the current view (i think you should use document.scolltop etc..). Also run an iframe site1:page2 passing the needed width as in the query string.

site1:page2:read the query string and find out the requested height, update parent.parent.document.getElementById("myframe").style.height

Nir Levy
If that works (I'm not sure because one of `parent` is in forbidden territory. Have you tested it?): *Clever* thinking. Hats off. Need to keep this in mind. However, this specific one can be solved easier because it's a subdomain.
Pekka
the parent.parent trick is working for me in production in all major browsers (not tested in IE8 but should work). be careful about using document.domain as it is not working in some versions of IE (i think IE6 Win XP SP1) - even for subdomains
Nir Levy
A: 

If it's just another subdomain, document.domain should give you the needed access between frames.

Pekka
Yes, if you do `document.domain = "mydomain.com";` from within the iFrame document *and* from within the main document, this should work. AFAIK, you can only set document.domain from within each document (you cannot set another frame's/window's domain property), and the only allowable change is to strip subdomains from it (i.e. you can change www.mydomain.com to mydomain.com, but not the other way around, nor can you change the domain completely)
Graza
note that document.domain does not work for all versions of IE
Nir Levy