Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  180] [ 7]  / answers: 1 / hits: 125166  / 13 Years ago, wed, january 11, 2012, 12:00:00

I've been following this tutorial - http://www.youtube.com/watch?v=R2hOvZ7bwXU, which explains how to use postMessage to securely pass a message between iframe and parent - you basically end up with something like this - http://html5demos.com/postmessage2


My problem is that I need it to work the opposite way round (child to parent) and don't know how to target the parent window.


this is my receiver code (in the parent):


function handleMsg(e) {
if(e.origin == "http://uc.dialogue.net") {
let blah = e.data;
alert(blah);
} else {
alert("error");
}
}
addEventListener("message", handleMsg, true);

and this is the sender function that is triggered by a simple form (in child):


   let text = document.querySelector('.srchInput').value;
window.parent.postMessage(text, "http://uc.dialogue.net");

Should I be targeting the parent in a different way?


Cheers
Paul


More From » html

 Answers
14
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) {
var key = e.message ? message : data;
var data = e[key];
//run function//
},false);


Got it to work with the above in the parent page and the following in the child page -
   



parent.postMessage(loadMyOrders,*);  //  `*` on any domain         


Code copied from here.


[#88079] Tuesday, January 10, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianod

Total Points: 667
Total Questions: 106
Total Answers: 92

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
lucianod questions
;