Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access elements of parent window from iframe

I have a page we can call parent.php. In this page I have an iframe with a submit form and outside of that I have a div with the id "target". Is it possible to submit the form in the iframe and when success refresh the target div. Say load a new page into it or something?

Edit: The target div is in the parent page, so my question is basically if you can make for an example a jQuery call outside the iframe to the parent. And how that would look?

Edit 2: So this is how my jquery code looks like now. It is in the of the iframe page. the div #target is in the parent.php

$(;"form[name=my_form]").submit(function(e){       e.preventDefault;   window.parent.document.getElementById('#target');   $("#target").load("mypage.php", function() {   $('form[name=my_form]').submit();   });   }) 

I don't know if the script is active cause the form submits successfully but nothing happens to target.

Edit 3:

Now I'm trying to call the parent page from a link within the iframe. And no success there either:

<a href="javascript:window.parent.getElementById('#target').load('mypage.php');">Link</a> 
like image 769
Paparappa Avatar asked Aug 11 '11 14:08

Paparappa


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 can an iframe communicate with its parent?

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.

How do I open an iframe external link in parent window?

To force a single link from iframe to open in the parent window: add target="_PARENT" within that links anchor tag. To force ALL links from iframe to open in the parent window: add the base tag with target="_PARENT" in the head section of the iframe html page.


1 Answers

I think the problem may be that you are not finding your element because of the "#" in your call to get it:

window.parent.document.getElementById('#target');  

You only need the # if you are using jquery. Here it should be:

window.parent.document.getElementById('target');  
like image 149
barclay Avatar answered Oct 13 '22 05:10

barclay