Basic tutorial on postMessage API in HTML5, html5postmessage
About postMessage
Although window. postMessage is html5, it supports IE8 +. If your website does not need to support IE6 or IE7, you can use window. postMessage. About window. postMessage, many friends said that it can support cross-origin. Good, window. postMessage is a direct data transfer between the client and the client, both cross-origin transmission and same-domain transmission.
Application scenarios
I just give a simple example of an Application Scenario. Of course, this function can be used in many places.
If you have a page where you can get some user information and click to enter another page, the other page does not get the user information by default. You can use window. postMessage uploads some user information to this page. (Of course, you need to consider security and other aspects .)
Code example
Sending information:
Copy the content to the clipboard using JavaScript Code
- // A new window is displayed.
- Var domain = 'HTTP: // haorooms.com ';
- Var myPopup = window. open (domain
- + '/WindowPostMessageListener.html', 'mywindow ');
- // Periodically send messages
- SetTimeout (function (){
- // Var message = 'current time is '+ (new Date (). getTime ());
- Var message = {name: "Site", sex: "male"}; // You can also transmit some data here, such as obj.
- Console. log ('transmitted data is '+ message );
- MyPopup. postMessage (message, domain );
- },1000 );
To delay, we usually use the timer setTimeout to delay and then use it again.
Accepted page
Copy the content to the clipboard using JavaScript Code
- // Listen for message feedback
- Window. addEventListener ('message', function (event ){
- If (event. origin! = 'HTTP: // haorooms.com ') return; // check whether the domain name is redirected.
- Console. log ('stored ed response: ', event. data );
- }, False );
For example, accept the page to get data
If iframe is used, the code should be written as follows:
Copy the content to the clipboard using JavaScript Code
- // Capture iframe
- Var domain = 'HTTP: // haorooms.com ';
- Var iframe = document. getElementById ('myiframe'). contentWindow;
- // Send a message
- SetTimeout (function (){
- // Var message = 'current time is '+ (new Date (). getTime ());
- Var message = {name: "Site", sex: "male"}; // You can also transmit some data here, such as obj.
- Console. log ('the transmitted data is: '+ message );
- // Send the message and target URI
- Iframe. postMessage (message, domain );
- },1000 );
Accept data
Copy the content to the clipboard using JavaScript Code
- // Respond to the event
- Window. addEventListener ('message', function (event ){
- If (event. origin! = 'HTTP: // haorooms.com ') return;
- Console. log ('message received ed: '+ event. data, event );
- Event. source. postMessage ('holla back youngin! ', Event. origin );
- }, 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, message sending window/iframe.
Origin-the origin URI (which may contain protocols, domain names, and ports) to verify the data source.
Data-the data sent by the sender to the receiver.
Call an instance
1. Create a Worker instance in the main thread and listen to onmessage events
Copy the content to the clipboard using JavaScript Code
- <Html>
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = iso-8859-1">
- <Title> Test Web worker </title>
- <Script type = "text/JavaScript">
- Function init (){
- Var worker = new Worker ('ute. js ');
- // The event parameter has the data attribute, Which is the result data returned by the Child thread.
- Worker. onmessage = function (event ){
- // Add the result returned by the sub-thread to the div
- Document. getElementById ("result"). innerHTML + =
- Event. data + "<br/> ";
- };
- }
- </Script>
- </Head>
- <Body onload = "init ()">
- <Div id = "result"> </div>
- </Body>
- </Html>
In the client's compute. js, the result is simply returned to the main thread through the postMessage method after repeated addition operations. The purpose is to wait for a while. During this period, the main thread should not be blocked. You can drag and drop the browser to expand or narrow the browser window and perform other operations to test this phenomenon. The result of this non-blocking main thread is what the Web Workers wants to achieve.
2. The postMessage method is called in compute. js to return the calculation result.
Copy the content to the clipboard using JavaScript Code
- Var I = 0;
- Function timedCount (){
- For (var j = 0, sum = 0; j <100; j ++ ){
- For (var I = 0; I <100000000; I ++ ){
- Sum + = I;
- }
- }
- // Call postMessage to send messages to the main thread
- PostMessage (sum );
- }
- PostMessage ("Before computing," + new Date ());
- TimedCount ();
- PostMessage ("After computing," + new Date ());