What are the MessageChannel and MessagePort Objects in HTML5?

HTMLWeb DevelopmentFront End Technology

While creating messageChannel, it internally creates two ports to send the data and forwarded it to another browsing context.

  • postMessage() − Post the message throw channel
  • start() − It sends the data
  • close() − it closes the ports

In this scenario, we are sending the data from one iframe to another iframe. Here we are invoking the data in function and passing the data to DOM.


var loadHandler = function(){
   var mc, portMessageHandler;
   mc = new MessageChannel();
   portMessageHandler = function(portMsgEvent){
      alert( portMsgEvent.data );
   mc.port1.addEventListener('message', portMessageHandler, false);
window.addEventListener('DOMContentLoaded', loadHandler, false);
Published on 06-Apr-2018 14:19:21