There are already a few similar questions but they tend to cover child-to-parent relationship.
I, on the other hand, would like to be able to get some properties of the IFrame's DOM. I don't want to manipulate anything. I only want to be able to read properties or DOM nodes. Things like attributes or contents.
AFAIK this is not possible unless I use something like window.postMessage
. As reasonable as this solution it's based on event listeners. This requires a listener to be defined inside the IFrame and this means changing the IFrame's code. I want to avoid that.
If you want to make a cross-domain Ajax call, you can (if you have access too) set target server's headers to allow that:
Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:POST,GET,DELETE,PUT,OPTIONS
Access-Control-Allow-Origin:*
This interestingly enough doesn't work for communication with an IFrame. So my question stands:
(How) can I access cross-domain IFrame's DOM properties?
When a page is running inside of an iframe, the parent object is different than the window object. You can still access parent from within an iframe even though you can't access anything useful on it. This code will never cause an error even when crossing origins.
To access cross-domain iframe, the best approach is to use Javascript's postMessage() method. This method provides a way to securely pass messages across domains.
Iframe: wrong document pitfallIt's not related to cross-origin things, but important to know. Upon its creation an iframe immediately has a document. But that document is different from the one that loads into it!
I cant even tell you how many times I've ran into problems like this.
Read this community wiki on circumventing the same-origin policy to find a solution that works for you. Its one of the best same-origin resources I've found on the internet.
Alex Sexton of yayQuery also put together a screencast on some different methods
i would give a shot to http://easyxdm.net/wp/ used it many times, quite easy to use and works in older browsers too
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With