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?
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.
The easiest way would be through the frames object on window like this:
window.frames[0].document.getElementById('ElementId').style.backgroundColor="#000";
If the two domains are completely seperate then it is impossible
It could be done with Chrome using --disable-web-security parameter... ;)
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.
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.
Yes, via the document.frames
array you can access iframes.
©2020 All rights reserved.