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.
receiveMessage
is using the variableorigin
that has never been set. So it returns. - Barmarconsole.log(event)
outside the function with theevent
parameter. What's that supposed to do? - Barmarorigin
should beevent.origin
, but why are you even bothering with that? It can never be*
, it's always a URL. - Barmarconsole.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. - Danielmessage
event listener. I usedsetTimeout
to delay the sending code by 1 second and it worked. - Barmar