92

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>
asked Aug 11, 2011 at 14:37
2
  • The target div is in the parent page or iframe? Commented Aug 11, 2011 at 14:43
  • its in the parent page, so my question is basicly if you can make for an example a jquery call outside the iframe to the parent. Commented Aug 11, 2011 at 14:45

4 Answers 4

152

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'); 
answered Jan 25, 2012 at 17:14
Sign up to request clarification or add additional context in comments.

5 Comments

What if the website container and the iframe are on different domains?
@Michelem I think you're out of luck; for you to have access to the parent, the iframe needs to have the same protocol and domain of the parent. That's a safeguard to prevent cross domain scripting.
@Michelem: The only way to communicate with the parent if the iframe and parent is on different domains, is using window.postMessage(). That's possible only if you have control over both ends, though. Documentation here: developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
@michelem is there any example how to access the parent element within iframe? we can do stuff with window.postMessage() but what is we want to access the element and use it in iframe?
It is late but example window.postMessage() is here based on reactjs dev.to/damcosset/…
21

You can access elements of parent window from within an iframe by using window.parent like this:

// using jquery 
window.parent.$("#element_id");

Which is the same as:

// pure javascript
window.parent.document.getElementById("element_id");

And if you have more than one nested iframes and you want to access the topmost iframe, then you can use window.top like this:

// using jquery
window.top.$("#element_id");

Which is the same as:

// pure javascript
window.top.document.getElementById("element_id");
answered Feb 21, 2016 at 14:08

1 Comment

Just a note, using jQuery is not the same as using getElementById. Might be confusing for new developers 👍
10

Have the below js inside the iframe and use ajax to submit the form.

$(function(){
 $("form").submit(e){
 e.preventDefault();
 //Use ajax to submit the form
 $.ajax({
 url: this.action,
 data: $(this).serialize(),
 success: function(){
 window.parent.$("#target").load("urlOfThePageToLoad");
 });
 });
 });
});
answered Aug 11, 2011 at 14:46

5 Comments

It works to submit the form through ajax, but the target div remains as it is from the beginning. It seems as it doesent work to communicate with parent.php from within the iframe.
If the iframe is in the same domain then you will not have any issues. Can you try to alert window.parent.$("#target").length in the success handler.
the iframe is in the same domain. Well i put that code in instead of window.parent.$("#target").load("urlOfThePageToLoad"); but it returned nothing. Where it sais "form" in your code, is it supposed to be the name of the form? Could that be it?
What is the name of the form? Put is as form[name=nameOfYourform] in place of form
What are you trying to do in your modified question?
8

I think you can just use window.parent from the iframe. window.parent returns the window object of the parent page, so you could do something like:

window.parent.document.getElementById('yourdiv');

Then do whatever you want with that div.

answered Aug 11, 2011 at 14:46

6 Comments

Im a jquery novice but should i put something like this in the ifram file?
$(document).ready(function(){ window.parent.document.getElementById('#target'); $("#target").load("file.php"); });
Probably more like: $(document).ready(function() { $(window.parent.document.getElementById("target")).load("file.php")); });
Im afraid this doesent work. The form gets submitted well but nothing happens to the target div. It remains as it is. Hmm..
Probably need to keep the form from submitting until the javascript that you want to run from the iframe finishes submitting. So it would be something like (sorry if there are mistakes in the code below, writing it in a code block like this is a pain): $(;"form").submit(function(e){ e.preventDefault; window.parent.document.getElementById('#target'); $("#target").load("file.php", function() { $('form').submit(); }); })
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.