


Detailed explanation of the basic usage of Communication API in HTML5_html5 tutorial skills
Cross-document message communication can ensure safe cross-source communication between iframes, tabs, and windows. It defines the postMessage API as the standard way to send messages. It is very simple to use postMessage to send messages. The code is as follows:
chatFrame.contextWindow.postMessage('Hello,world','http://www.example.com');
When receiving messages, just click on the page Add an event handling function. When a message arrives, the source of the message is checked to determine whether to process the message.
The message event is a DOM event with data and origin attributes. The data attribute is the actual message delivered by the sender, and the origin attribute is the sending source.
The postMessage API is not only capable of communicating between documents of the same origin, but it is also useful when the browser does not allow non-original communication. Due to its consistency and ease of use, postMessage is also recommended when communicating between documents of the same origin. The postMessage API should always be used when communicating in a JavaScript environment, such as when communicating with HTML5 Web Workers.
1.1 Understanding origin security
HTML5 Rongguang introduces the concept of origin to clarify and improve domain security. An origin is a subset of addresses used to establish trust relationships on the network. The source consists of rules (scheme), host (host), and port (post).
Path is not considered in the concept of source.
HTML5 defines serialization of sources. Sources appear as strings in APIs and protocols.
PostMessage security rules ensure that messages are not delivered to unintended source pages. When sending a message, it is the sender who specifies the source of the receiver. If the window used by the sender to call postMessage does not have a specific origin (for example, the user jumped to another site), the browser will not deliver the message.
Similarly, when receiving a message, the sender's source is also included as part of the message. To avoid forgery, the source of the message is provided by the browser. The receiver can decide which messages to process and which to ignore. We can keep a whitelist and tell the browser to only process messages from trusted sources.
It is best to never evaluate strings from third parties. Furthermore, avoid using the eval method to process application-internal strings. JSON can be consumed via window.JSON or json,.org parsers.
1.2 Browser support for cross-document message communication
A common approach is to detect whether the withCredentials attribute exists in the XMLHttpRequest object:
- var xhr = new XMLHttpRequest(); if (typeof xhr.withCredentials === undefined) { //Does not support cross-origin XMLHttpRequest } else { //Supports cross-origin XMLHttpRequest }
Tips The MessageEvent interface defined by HTML5 is also part of HTML5 WebSockets and HTML5 WebWorkers. The communication function of HTML5 uses the same API for receiving messages as the MessageEvent interface. Other communication APIs, such as EventSource API and Web Workers, also use the MessageEvent interface to deliver messages.
1.4 Create an application using postMessage API
Send a message
You can send a message by calling the postMessage() function in the window object of the target page. The code is as follows:
- window.postMessage("Hello, world", "porta");
- document.getElementsByTagName("iframe")[0].contentWindow.postMessage("Hello, world", "cha");
When receiving messages, you only need to add an event handler to the page. When a message arrives, the source of the message is checked to determine whether to process the message.
- window.postMessage("Hello, world", "porta");
The source consists of rule (scheme), host (host), and port (port).
For example: due to different rules (such as https and http), the source of the page is different.
The path is not considered in the concept of source. For example: instead of just paths, they are the same source.
Sources appear as strings in APIs and protocols.
- var originWhiteList = ["porta", "game" , ""]; function checkWhiteList(origin) { for (var i=0; i
if (origin === originWhiteList[i]) { return true; } } return false; } function messageHandler(e) { if (checkWhiteList(e.origin)) { processMessage(e. data); } else { //Ignore messages from unknown sources } }
2 XMLHttpRequest Level2
As an improved version of XMLHttpRequest, XMLHttpRequest Level2 has greatly improved its functionality. Mainly focused on two aspects:
(1) Cross-source XMLHttpRequests; Origin Resource Sharing (cross-origin resource sharing) implements cross-origin XMLHttpRequests.
Cross-origin HTTP requests include an Origin header to provide the server with source information of the HTTP request. The header is protected by the browser and cannot be modified by application code. Essentially, it has the same effect as the origin attribute of a message event in cross-document message communication.
CORS specification requires that some sensitive behaviors, such as requests to apply for certificates or OPTIONS preflight requests other than GET and POST, must be sent by the browser to the server to determine whether this behavior can be supported. and permission, which means that successful communication may need to be supported by a server with CORS capabilities.
2.2 Progress Events
One of the most important API improvements in the new version of XMLHttpRequest is the addition of response to progress.
XMLHttpRequest Level2 uses a meaningful name Progress to name the progress event.
3 Advanced functions
3.1 Structured data
Early versions of postMessage only support strings. Later versions supported other data types such as JavaScript objects, canvas imageData, and files. Due to differences in specification support between different browsers, support for different object types is also different.
3.2 Framebusting
Framebusting technology can be used to ensure that certain content is not loaded into jframe. The application first detects whether the window it is in is the outermost window (window.top). If not, it jumps out of the frame containing it. The code is as follows:
JavaScript Code
Copy content to clipboard
- if(window!=window.top)
- {
- window.top.location=location;
- }
Browsers that support new binary APIs (such as Typed Array) can use XMLHttpRequest to send binary data. Level 2 specification supports calling the send() method to send Blob and ArrayBuffer objects
- var a = new Uint8Array([8,6,7,5,3, 0,9]); var xhr = new XMLHttpRequest(); xhr.open("POST", "/data/ ", true); console.log(a); xhr.send(a.buffer);

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
