I have iframe on a page, the iframe and the parent page are in different domain, can a javascript code on the parent page access elements inside this iframe?
7 Answers
It should not be able to if the pages are from different domains, the browsers security sandbox should prevent this type of access. It might work when the two pages are from different sub domains of the same domain, but that can and will differ between browsers (and possibly even versions of the same browser).
Accessing the child iframe might work, but the other way around will most certainly not work.

- 6,682
- 1
- 26
- 35
-
i am talking about access the child iframe elements, it should work? – Amr Elgarhy Apr 08 '09 at 11:37
-
these are sites i am creating, and want them all to call one service. – Amr Elgarhy Apr 08 '09 at 12:28
-
You may be better off using an XMLHttpRequest object to call your service. All frames will then be able to access the service for themselves and they won't need any communication between them. – Gerco Dries Apr 08 '09 at 15:10
The easiest way would be through the frames object on window like this:
window.frames[0].document.getElementById('ElementId').style.backgroundColor="#000";

- 21,728
- 13
- 62
- 101
-
3If child frame is in a different domain you will get `Error: Permission denied to access property 'document'` – lulalala Nov 01 '12 at 10:17
-
2The question states that the iframe is on another domain, so this won't work. – M. Jahedbozorgan Jan 10 '13 at 19:01
If the two domains are completely seperate then it is impossible

- 3,117
- 1
- 32
- 32
-
easyxdm can be used just when you have control over both domains. – M. Jahedbozorgan Jan 10 '13 at 19:00
Use jQuery.postMessage plugin http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html
Browsers Tested Internet Explorer 6-8, Firefox 3, Safari 3-4, Chrome, Opera 9.

- 26,542
- 16
- 152
- 170
You can use CORS to achieve this, specifically changing the 'document.domain' value to match in both parent & child. But, please undestand the security implications of this, and that you require access to the source code of both parent & child documents.
This is especially important if you own multiple sub domains. I use this technique all the time, so I know it works, even with SSL.

- 1,760
- 16
- 21
Yes, via the document.frames
array you can access iframes.

- 4,646
- 4
- 35
- 62
-
1You're ignoring the x-domain part of the question which is far more relevant. Also document.frames is non-standard, use window.frames which has wider support – annakata Apr 08 '09 at 11:38
-
6-1 First of all, it's window.frames. Secondly, it still will not work if the pages are on different domains. – Josh Stodola Apr 09 '09 at 14:33