Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

How to pass variables and/or launch functions from child pages to parent in iFrames?

I am encountering many troubles in passing the document.body.scrollHeight of an iFrame to the parent page in order to set its container size: mainly I get "denied access" errors when using "parent" or "window.parent" functions.
Could you explain me why and how to make this feature work?
0
Sar1973
Asked:
Sar1973
  • 7
  • 3
  • 2
  • +1
1 Solution
 
Sar1973Author Commented:
Does this method I have found on the web apply?

(onchildevent)
      // Send the message "Hello" to the parent window
      // ...if the domain is still "davidwalsh.name"
      parent.postMessage(this.document.body.scrollHeight,"http://www.parent.page");

Since we now have hold of the window, we can postMessage to it:
// Create IE + others compatible event handler
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

// Listen to message from child window
eventer(messageEvent,function(e) {
  console.log('parent received message!:  ',e.data);
},false);
0
 
ahoffmannCommented:
i.g. frames are handled like windows, hence they follow the same origin policy
if both windows (parent and your iframe) use content from the same domain i.e. some.tld, then you need to set each window's document.domain to the corresponding value
if they are from different domain, they cannot access each other, you need a proxy page then
0
 
Michel PlungjanIT ExpertCommented:
AHoffmann - it is a cross origin messaging

http://ajaxian.com/archives/using-html-5-postmessage
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ahoffmannCommented:
oops, sorry not reading proper and giving old fashioned answer :-/
0
 
virmaiorCommented:
This question is identical to:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_27556193.html

asked by the same poster and still open.
0
 
Sar1973Author Commented:
@mplungjan: I will test the addEventListener monday.
@Mr.Virmaior: unfortunately, I did not receive this code adapted to the question you mention. Would it be something like this? If so, where to paste formulas? Only in the frame page or also in the parent page?

(frame page body) onclick(function(this.name)) {  
  var myFRMHGT = document.getElementById(this.name).body.offsetHeight;  
  window.parent.frames['myFrame'].postMessage(myFRMHGT);        
}  
   
var onmessage = function(this.name) {  
  var data = document.getElementById(this.name).data;  
  var origin = document.getElementById(this.name).origin;  
  document.getElementById('myFrame').style.height = data;  
}
     
if (typeof window.addEventListener != 'undefined') {  
  window.addEventListener('message', onmessage, false);  
} else if (typeof window.attachEvent != 'undefined') {  
  window.attachEvent('onmessage', onmessage);  
}
0
 
Sar1973Author Commented:
One nmore question: the if(e.origin == 'http://origin-domain.com') { declaration can only be done when the page in online? I actually get an error if I type only the page name.
0
 
Sar1973Author Commented:
I've spotted like this:
PARENT PAGE (FRAME CONTAINER)
function getHeight(h) {
      if (h..origin == "http://www.domain.com")  {
            document.getElementById("myFrame").style.height = h.data + "px";  
      }
}
if (window.addEventListener) {
      window.addEventListener("message", receiveHeight,false);  
} else {
      window.attachEvent("onmessage", receiveHeight);
}

FRAME PAGE (onclick event="setTimeout('sendHeight(this.document.body.offsetHeight)',100)")
function sendHeight(h) {
var h1=this.document.body.offsetHeight;
if (h !=h1) {
      var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined);
      if (typeof target!= 'undefined') {
            target.postMessage(h1, '*');
            }  
            if (window.addEventListener) {
                  window.addEventListener("load", sendHeight, false);  
            } else {
                  window.attachEvent("load", sendHeight);
            }
      }
}
0
 
Sar1973Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Sar1973's comment #37766846

for the following reason:

Was an auto answer.
0
 
virmaiorCommented:
This code is not functionally different from the code I provided you with in http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Q_27556193.html.
0
 
Sar1973Author Commented:
Not at all: I solved it using a postMessage method, not encapsulating.
0
 
virmaiorCommented:
I will make a note never to participate in questions you ask again.  That way, we will both be happier.
0
 
Sar1973Author Commented:
I have not deprecated your answer or said you are not reliable as expert: just appointed that did not fit to my request, which was to pass the height variable from frame to parent (while your code does not).
I do not understand then why I should enter in your "black list".
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now