Friday, October 10, 2008

Automatically adjust height of IFrame using Javascript

A simple and natural requirement, but unfortunately it may or may not work as you expect.
What matters here is security. If src of an iframe does not access external address, it works well. Otherwise, nothing will solve the problem except that the user lowers the security level. Javascript can not access information of an IFrame whose content is hosted by a third-party site.

Let us assume that src of an iframe does not point to an external resource, following code can be used:

var ele =document.getElementById(frameid); 
ele.style.height = ele.contentWindow.document.body.scrollHeight

You may wonder why document.frames[frame_name] is not used here. Anyway, I tried it on Firefox 3, it did not work. As you know, compatibility is a real pain for web developers.

One possible solution is to set up a proxy server which relays the requests/responses.

No comments: