In HTML5, postMessage is used to transmit data between two webpages. html5postmessage

Source: Internet
Author: User

In HTML5, postMessage is used to transmit data between two webpages. html5postmessage

It is estimated that few people know that there is a window. postMessage API in HTML5 APIS. Window. postMessage allows programmers to send data information across two windows/frames. Basically, it is like cross-origin AJAX, but it is not the interaction between the browser and the server, but the communication between the two clients. Let's take a look at how window. postMessage works. All browsers except IE6 and IE7 support this function.

Data sender

The first thing we need to do is create the communication initiator, that is, the data source "source ". As the initiator, we can open a new window or create an iframe to send data to the new window. For simplicity, we send the message every 6 seconds and then create a message listener, listen to the feedback from the target window.

Copy the content to the clipboard using JavaScript Code
  1. // A new window is displayed.
  2. Var domain = 'HTTP: // scriptandstyle.com ';
  3. Var myPopup = window. open (domain
  4. + '/WindowPostMessageListener.html', 'mywindow ');
  5. // Periodically send messages
  6. SetInterval (function (){
  7. Var message = 'Hello! The time is: '+ (new Date (). getTime ());
  8. Console. log ('blog. local: sending message: '+ message );
  9. // Send the message and target URI
  10. MyPopup. postMessage (message, domain );
  11. },6000 );
  12. // Listen for message feedback
  13. Window. addEventListener ('message', function (event ){
  14. If (event. origin! = 'HTTP: // scriptandstyle.com ') return;
  15. Console. log ('stored ed response: ', event. data );
  16. }, False );

Here I use window. addEventListener, but this is not acceptable in IE, because IE uses window. attachEvent. If you do not want to determine the browser type, you can use some tool libraries, such as jQuery or Dojo.

If your window pops up normally, we send a message-you need to specify the URI (protocol, host, port number, and so on if necessary). The message receiver must be on this specified URI. If the target window is replaced, the message will not be sent.

We also created an event listener to receive feedback. It is very important to verify the URI of the message source! Only when the target party is valid can you process the messages sent by the target party.

If iframe is used, the code should be written as follows:

Copy the content to the clipboard using JavaScript Code
  1. // Capture iframe
  2. Var domain = 'HTTP: // scriptandstyle.com ';
  3. Var iframe = document. getElementById ('myiframe'). contentWindow;
  4. // Send a message
  5. SetInterval (function (){
  6. Var message = 'Hello! The time is: '+ (new Date (). getTime ());
  7. Console. log ('blog. local: sending message: '+ message );
  8. // Send the message and target URI
  9. Iframe. postMessage (message, domain );
  10. },6000 );

Make sure that you are using the contentWindow attribute of iframe, rather than the Node object.

Data receiving end

The following is the data receiving end page. The receiver window contains an event listener that listens to "message" events. Similarly, you also need to verify the address of the message source. A message can come from any address. Make sure that the message to be processed comes from a trusted address.

Copy the content to the clipboard using JavaScript Code
  1. // Respond to the event
  2. Window. addEventListener ('message', function (event ){
  3. If (event. origin! = 'HTTP: // davysh. name') return;
  4. Console. log ('message received ed: '+ event. data, event );
  5. Event. source. postMessage ('holla back youngin! ', Event. origin );
  6. }, False );

The above code snippets provide feedback to the source to confirm that the message has been received. The following are several important event attributes:

Source-Message Source: the message sending window/iframe.
Origin-The URI (which may contain the protocol, domain name, and port) of the message source to verify the data source.
Data-The data sent by the sender to the receiver.

These three attributes are required for message transmission.

Use window. postMessage

Like other very web technologies, if you do not verify the legitimacy of the data source, using this technology will become very dangerous; you are responsible for the security of your application. Window. postMessage is like PHP relative to JavaScript technology. Window. postMessage is cool, isn't it?

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.