views:

2374

answers:

6

The question of how to make your iframe fit 100% of your content (using JavaScript) has been answered on the forum already... for iframes displaying content from the same domain only.

My questions: Is it possible to resize an iframe to fit the content when the iframe src attribute is for a page outside of the domain of the page containing the frame? The browser security features surrounding cross-site scripting prevent me from accessing the document object for the iframe.

I'm looking for any creative solution. One of my thoughts was to somehow detect that scrollbars were visible on the iframe, and to incrementally increase the height until they weren't. Unfortunately I can't find any way to detect visible scrollbars either. Ideas?

Please note that I am looking for a client-side solution.

Responses:


Dave

The problem still exists - I don't know the height of the remote document ahead of time, so I can't set it from a database or querystring value.


dusoft

100% on the iframe in CSS will not set it to 100% of the content inside the iframe, but relative to the the page or containing element (i.e., a div or something).

A: 

It sounds like you are trying to work around a security feature that was put in place for a good reason...

Does the resizing have to be through script? You could redirect to a page where the size of the iframe is taken from a querystring, or a database, or config file, etc.

Dave Swersky
A: 

what about using 100% in CSS? scrollbars can be disabled in iframe, but then user won't be able to scroll.

that's the JS/XSS prevention, so get over it and try to pull the content on the server side and serve it via local connection.

dusoft
+1  A: 

Is it possible to resize an iframe to fit the content when the iframe src attribute is for a page outside of the domain of the page containing the frame?

It is possible to get around the cross-site limitation and have an iframe set to the height of it's content, but it can be a bit awkward to get it to work.

The solution involves working around the security model by having the iframed content itself iframe a page on the the same server as the parent page, with which it can communicate. I've answered a similar question to this before here, which goes in to a fair bit of detail on the ins and outs of setting it all up.

The caveat on the described approach is that you must have means of getting your iframed content to itself iframe a page on the parent server. This is usually achieveable when the iframed content is there as part of an agreed partnership between sites, but YMMV.

ConroyP
A: 

You can try this solution which is documented here: http://blog.johnmckerrell.com/2006/10/22/resizing-iframes-across-domains/

brozo