Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 715
Michal M Avatar asked Jul 12 '11 14:07

Michal M


People also ask

Can iframe access parents?

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.

How do I get cross domain iframe content?

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.

Is iframe cross-origin?

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!


2 Answers

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

like image 161
Greg Guida Avatar answered Nov 28 '22 21:11

Greg Guida


i would give a shot to http://easyxdm.net/wp/ used it many times, quite easy to use and works in older browsers too

like image 20
user1555320 Avatar answered Nov 28 '22 19:11

user1555320