Wed Feb 01 2017
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
var mc = new MessageChannel();
mc.port1.onmessage = function(e){ 
    console.log(e);
    mc.port2.postMessage('hi back');
};

//You would normally do this from a WebWorker or an iframe
window.addEventListener('message', function(e){
    console.log(e);
    e.ports[0].postMessage('hi');
    
});

window.postMessage('from window', '*', [mc.port2]);