I'm trying to write a JavaScript application that will load a button up in an iFrame on a page. This application will read the parent document and strip out all images, apply minimal styling and reload the parent with this newly marked up page.
The problem I am having is reading and writing the parent document from the iFrame. Does anyone know of a way to achieve this? I've read up a little on cross-domain messaging but am unsure of the alternatives and which would be best.
Thanks in advance.
1) click on images link in iframe changes Parent page, click on another iframe image link changes Parent to another page (see below). can you please post the code you're currently using, in addition to the link you provided? You need to name the parent and use the target attribute.
It's still possible to access the parent from within a frame provided that the domains match. The variables parent and top can be overwritten (usually not intended). It's safer to use window.
All you have to do is first dispatch an event from the iframe to the parent that notifies the parent that the iframe is loaded (essentially a "ready message"). The parent will be listening for messages and if it receives the "ready message" event, it can then reply to the iframe with whatever message you want to send.
You can reference the parent either by using parent or using the top variable that always points to the outer most document.
So for accesing the first div in the parent window
var d = parent.document.getElementsByTagName("div")[0];
But as noted already in the comment, both the outer document and the document in the iframe need to be from the same domain or you will be blocked for security reasons.
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