I'm sure it's just a problem with my syntax but I am trying to send a variable to an iframe (for colorbox to use). For the time being I am accepting any domains on both ends (just to get it to work). Here is the js for the sending page:
$(document).ready(function() {
if(window.location.hash) {
var urlimage = window.location.hash.substring(1);
targetiframe = document.getElementById('wbgallery').contentWindow;
targetiframe.postMessage(urlimage, "*");
console.log(urlimage);
}
});
And here is the receiving page:
$(document).ready(function() {
window.addEventListener('message',receiveMessage);
console.log(event);
function receiveMessage(event) {
if (origin !== "*")
return;
inbound = event.data;
console.log(inbound);
}
});
I see the console log for urlimage and can see an event but nothing for inbound. I'm using Mozilla's explanation to try and work it all out.
I'm sure it's just a problem with my syntax but I am trying to send a variable to an iframe (for colorbox to use). For the time being I am accepting any domains on both ends (just to get it to work). Here is the js for the sending page:
$(document).ready(function() {
if(window.location.hash) {
var urlimage = window.location.hash.substring(1);
targetiframe = document.getElementById('wbgallery').contentWindow;
targetiframe.postMessage(urlimage, "*");
console.log(urlimage);
}
});
And here is the receiving page:
$(document).ready(function() {
window.addEventListener('message',receiveMessage);
console.log(event);
function receiveMessage(event) {
if (origin !== "*")
return;
inbound = event.data;
console.log(inbound);
}
});
I see the console log for urlimage and can see an event but nothing for inbound. I'm using Mozilla's explanation to try and work it all out.
Share Improve this question asked Nov 7, 2016 at 8:53 DanielDaniel 7922 gold badges6 silver badges24 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 16You're sending the message before the page in the iframe has loaded, so the message
listener hasn't been established yet.
You can have the iframe send a message to the parent when it's ready, and then send the message after this.
Parent code:
$(document).ready(function() {
if (window.location.hash) {
var urlimage = window.location.hash.substring(1);
var targetiframe = document.getElementById('wbgallery').contentWindow;
$(window).on("message", function(e) {
targetiframe.postMessage(urlimage, "*");
console.log(urlimage);
});
}
});
Iframe code:
$(document).ready(function() {
$(window).on('message', function(event) {
inbound = event.data;
console.log(inbound);
});
window.parent.postMessage("ready", "*");
});
receiveMessage
is using the variableorigin
that has never been set. So it returns. – Barmar Commented Nov 7, 2016 at 9:07console.log(event)
outside the function with theevent
parameter. What's that supposed to do? – Barmar Commented Nov 7, 2016 at 9:07origin
should beevent.origin
, but why are you even bothering with that? It can never be*
, it's always a URL. – Barmar Commented Nov 7, 2016 at 9:09console.log(event)
was just to see if anything showed up. Saw someone else do it on a SE question and thought I'd try it. – Daniel Commented Nov 7, 2016 at 9:14message
event listener. I usedsetTimeout
to delay the sending code by 1 second and it worked. – Barmar Commented Nov 7, 2016 at 9:30