views:

3179

answers:

3

I control the content of an iframe which is embedded in a page from another domain. Is there any way for javascript in my iframe to make changes to the parent's DOM?

For example, I would like to have my iframed script add a bunch of html elements to the parent DOM. This seems like a pretty tall order - thoughts?

Edit: There exists a technique called "Fragment ID Messaging" which might be a way to communicate between cross-domain iframes.

Edit: Also, Firefox 3.5, Opera, Chrome (etc) seem to be adopting the html5 "postMessage" api, which allows secure, cross-domain data transmission between frames, iframes and popups. It works like an event system. IE8 supports this feature, apparently, which is perhaps a little surprising.

A: 

By using parent?

e.g. parent.document.getElementById('div1').style.display = 'none';

DmitryK
probably not a cross domain solution though
Dhana
And if you want to create new elements consider using document.createElement();
DmitryK
if it works whether this is cross-domain or not depends on where the javascript that makes changes live. If it lives in the parent frame then you can make changes there.
DmitryK
+7  A: 

Hate to say it but I'm like 99% sure that ain't happenin' directly because of security.

You can try it out here.

Andy Gaskell
Thanks, that link demonstrates the problem very well.
aaaidan
PS. You ruined my day, but thanks. :P
aaaidan
hey man, it's what I do ;)
Andy Gaskell
A: 

I guess you will run to security problems without using a proxy. Proxies can be very to use. You can try one of those:

(1) a PHP based proxy (be careful cause there are a lot of ads between useful links)

(2) a Apache .htaccess proxy - just create a subdirectory proxy in your domain and put there a .htaccess file containing:

RewriteEngine on
RewriteRule ^(.*)$ http://picasaweb.google.com/$1 [P,L]

Put the other domain name in place of picasaweb.google.com

Personally I prefer using the Apache proxy

warpech
Thanks for your answer, warpech. I think my edits confused my original question though, which was asking how I can alter the dom on a parent page from within an iframe from another domain. The short answer seems to be "you can't". So I'm now exploring inter-frame communication methods, and a server-side proxy is certainly one of those. Thanks!
aaaidan