


Detailed graphic and text explanation of postMessage API in H5 Detailed introduction
Regarding postMessage, although it is a function of html5, it supports IE8+. If your website does not need to support IE6 and IE7, you can use window.postMessage. It can be transferred across domains or within the same domain.
I am just giving a simple application scenario. Of course, this function can be used in many places.
If you have a page and get part of the user information on the page, click to enter another page. The other pages cannot get the user information by default. You can pass part of the user information to the page through window.postMessage. in this page. (Of course, you have to consider security and other aspects.)
Code example
Send message:
JavaScript Code copy content to clipboard
1 2 3 4 5 6 7 8 9 10 11 12 |
|
To To delay it, we usually use the timer setTimeout to delay it before sending it.
Accepted page
JavaScript Code copies content to the clipboard
//Listen to message feedback
1 2 3 4 |
|
As shown below, the accepted page gets data
If you are using iframe, the code should be written like this:
JavaScript Code copies the content to the clipboard
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Accepts data
JavaScript Code copies the content to the clipboard Board
1 2 3 4 5 6 |
|
The code snippet above is to feed back information to the message source and confirm that the message has been received. The following are several important event attributes:
source – message source, message sending window/iframe.
origin – URI of the message source (may include protocol, domain name and port), used to verify the data source.
data – Data sent by the sender to the receiver.
Call the instance
Create a Worker instance in the main thread and listen to the onmessage event
JavaScript Code copies content to the clipboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
In the client's compute.js, it simply repeats the sum operation multiple times, and finally returns the result to the main thread through the postMessage method. The purpose is to wait for a period of time. During this period, the main thread should not be blocked. Users can test this phenomenon by dragging the browser, enlarging or shrinking the browser window, etc. The result of this non-blocking main thread is what Web Workers want to achieve.
Calling the postMessage method in compute.js returns the calculation result
JavaScript Code copies the content to the clipboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
I believe after reading these cases You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
Implementation steps of using JS to operate HTTP Cookies
html Implementation steps of dotted border line
What is the common syntax of AJAX
The above is the detailed content of Detailed graphic and text explanation of postMessage API in H5 Detailed introduction. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
