Meteor overwrites events.data in window.addEventListener

I want to pass the referer from a parent window to an iframe.

parent window:

 var src = document.getElementById('Iframe');
 src.contentWindow.postMessage("message", 'placeholderURL');

meteor app in iframe:

window.addEventListener('message', function(event) {
...

Getting the data with event.data shows Meteor._setImmediate.0.9801140476483852.1 in the console.

Any idea? It works in non meteor apps.

thanks

Take a look at https://github.com/meteor/meteor/blob/devel/packages/meteor/setimmediate.js; this should help explain what you’re seeing.

Thanks. But i don´t see a solution. Can you help me out?

Sure - Meteor’s “message” event listener and your custom “message” event listener are interfering with each other. Meteor._setImmediate() in particular is causing you grief. Long story short though, if you just want to pass the referrer from the parent window to the iframe, why don’t you use URL parameters on the iframe URL instead? So in your parent window:

<iframe id="iframe" src="http://someurl.com"></iframe>
<script>
  var iframe = document.getElementById('iframe');
  iframe.src = iframe.src + '?placeholderURL=someUrl';
</script>

You can then pull the custom parameters out of the URL in your iframe based Meteor app:

console.log(window.location.search);

Thank you. I had something similar in mind, but thought there may be a meteor way.

Well, if you do want to use events you can always just make sure your event isn’t conflicting with Meteor “message” events. So something like this will work.

Parent window (make sure you’re firing the event after the iframe has loaded):

<iframe id="iframe" src="http://someurl.com" onload="postIt();"></iframe>
<script>
  function postIt() {
    var iframe = document.getElementById('iframe');
    iframe.contentWindow.postMessage('some message!', 'http://someurl.com');
  }
</script>

Somewhere in your Meteor iframe based app:

window.addEventListener('message', function (event) {
  // Make sure you're not trying to handle Meteor._setImmediate events
  if (event.data.indexOf('Meteor._setImmediate.') === -1) {
    console.log(event.data);
  }
});
2 Likes