Thursday, July 16, 2009

Accessing child iFrame properties In Internet Explorer 6, 7

We got that case:

< iframe src="/some-url-on-our-site.html?ala=bala&bate=boiko-borisov" width='333' height='333' id='portal'>
< /iframe>

function accessIframe(){
alert( "accessIframe()" );
if( testIframe.contentWindow.document ){
alert("got the iframe1");
if( testIframe.contentWindow.document.body )
alert("got the iframe2");

When we call accessIframe() in Firefox or Safari, everithing goes on fine. But when the above is executed on Internet Explorer, a javascript exception is thrown:

Access is denied.

The cause can be trying to access a iframe content wich is not on same domain, but in my case it was not that. (there are many content for domein relaxing on the net)
The problem is that the function has to be called when the iframe is loaded. So the working code in all browsers has to be called with a delay:

setTimeout( "accessIframe()", 20000 );

That is not the best solution but ilustrate how can you workaround the problem. For the final solution you cant try many times to access the iframe and in the bad case, you can catch the mentioned exception.