i have 2 sites, and i want to use javascriptpostMessage
between them.
on my main site i write the following code in an emty html file :
<html>
<script>
window.addEventListener("message",listener_,false);
function listener_(event) {
console.log('ok!');
<html>
<script>
window.addEventListener("message",listener_,false);
function listener_(event) {
console.log('ok!');
//console.log(event);
//console.log(event.data);
}
</script>
</html>
and in other site that i want to call the postmessage from the i write the following code:
<script type="text/javascript">
window.onload = function() {
testfunction();
};
function testfunction(){
var childWin = window.open(".html","child");
childWin = postMessage('message','*');
console.log(TipaltiIframeInfo.height);
}
</script>
but it doesn't work after a lot of tries. i mean console.log('ok!'); or console.log(event); console.log(event.data);
doesn't trigger on console of main site,
what to do? thanks
i have 2 sites, and i want to use javascriptpostMessage
between them.
on my main site i write the following code in an emty html file :
<html>
<script>
window.addEventListener("message",listener_,false);
function listener_(event) {
console.log('ok!');
<html>
<script>
window.addEventListener("message",listener_,false);
function listener_(event) {
console.log('ok!');
//console.log(event);
//console.log(event.data);
}
</script>
</html>
and in other site that i want to call the postmessage from the i write the following code:
<script type="text/javascript">
window.onload = function() {
testfunction();
};
function testfunction(){
var childWin = window.open("http://my-main-site./indexjava2.html","child");
childWin = postMessage('message','*');
console.log(TipaltiIframeInfo.height);
}
</script>
but it doesn't work after a lot of tries. i mean console.log('ok!'); or console.log(event); console.log(event.data);
doesn't trigger on console of main site,
what to do? thanks
Share Improve this question asked Jun 26, 2017 at 23:49 anonanon 4631 gold badge8 silver badges15 bronze badges 3-
1
In the first code block there is an unnecesarry
<script>
tag and there is a<html>
tag in script tag. Fix those first. – Ali Somay Commented Jun 26, 2017 at 23:53 -
1
just to get the obvious out of the way.. your posted code shows two listener callbacks with an opening
<script>
tag between them. That script tag will cause an error and neither work. Is this just a typo while posting here, or is this what you really have on your page? – CrayonViolent Commented Jun 26, 2017 at 23:54 - 1 Hopefully my answer is prehensive enough that you can get it working now :) – Clonkex Commented Jun 27, 2017 at 0:48
1 Answer
Reset to default 5Aside from the fact that you've got a <script>
and an <html>
tag in the middle of your code for the receiving page and you're defining and adding the event listener twice, you're also not use postMessage
correctly. Instead of this:
childWin = postMessage('message', '*');
...it should be this:
childWin.postMessage('message', '*');
If you want to learn more about postMessage
, read this.
The other issue is that the message won't be received by the newly-opened page unless the page is opened before the message is sent. You're trying to send the message immediately after opening the new page, and the message is reaching the new page before the event listener is added. You could get around this with a setTimeout
, but if the new page takes longer to load then this might also be unreliable.
// This is what NOT to do:
setTimeout(function() {
childWin.postMessage('message', '*');
}, 1000);
Instead, the better way is for the child page to tell the parent page when it's loaded. Then the parent and child can municate reliably.
Here is the full corrected code, sending page first:
<script>
var childWin;
window.addEventListener('message', messageListener, false);
function messageListener(event) {
if(event.data == 'ready') {
childWin.postMessage('hello second page', '*');
}
}
window.onload = function() {
childWin = window.open('http://my-main-site./indexjava2.htm', 'child');
};
</script>
And the receiving page:
<script>
window.addEventListener('message', messageListener, false);
function messageListener(event) {
console.log(event.data);
}
window.opener.postMessage('ready','*');
</script>