3

I want to send some event/data from parent window to its child window. How can I do so?

I have tried postMessage, but it didn't work.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Ajay Pratap
  • 397
  • 2
  • 12

2 Answers2

5

It has worked now, earlier I was doing some syntax mistake. Here is the correct code for the same.

//parent Window
childWindow=window.open("http:/localhost/abcd.php","_blank","width=500,height=500");
childWindow.postMessage('message',"http://localhost:80/child.php");


//child Window
var newUrl='';
window.addEventListener(
 "message",
 function(e) { 

console.log(e.data);//your data is captured in e.data 
}, false);
Ajay Pratap
  • 397
  • 2
  • 12
0

If the child window you're referring to is an iFrame, then postMessage is the method you're stuck with.

Without seeing any of the code you've tried, it's hard to say what about your particular usage of postMessage isn't working – however, here is a link to a working postMessage example from an MDN article, as well as an article with a fully functional example you can dissect the code from.

One of these should be able to show you why your implementation isn't working.

wosevision
  • 123
  • 10