I open 1.htm as a http://127.0.0.1/1.html
1.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<iframe id="ifr" src="http://localhost/2.html" width="100%" height="300">
</iframe>
<script>
iframe=document.getElementById("ifr");
iframe.contentWindow.postMessage("hello there", "http://localhost");
</script>
</body>
</html>
2.html
<!DOCTYPE html>
<html>
<head>
<script>
window.addEventListener("message", function(event) {
alert(hi);
if (event.data === "hello there" ) {
alert("Hi" + event.origin);
}
}, false );
</script>
<head>
<body>
Hello world
</body>
"
but I have that error: "Unable to post message to http://localhost. Recipient has origin http://127.0.0.1/
that's simple example. finally, I need structure like that: On domain "A", I have iframe and it's src is page of domain "B". in iframe, there is button. when i click to that button, which is shown inside iframe, I need to call window.addEventListener of domain "A" how can i do that?