Cross-domain IFrame DOM properties access from parent's JavaScript [duplicate]
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 al开发者_如何学Golow 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?
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
精彩评论