Common JS knowledge points and js knowledge points

Source: Internet
Author: User

Common JS knowledge points and js knowledge points

1. CORS

CORS (CORS-Origin Resource Sharing), the basic idea is to use a custom HTTP header to allow the browser to communicate with the server, thus determining the request or response success or failure. That is, an additional Origin header is appended to the request, which contains the source information (protocol, domain name, and port) of the Request page, so that the server can decide whether to respond to the request based on this header.

2.doc ument. domain

Set document. domain on the page to the same value, and the pages can access each other's JavaScript objects.

Note:

You cannot set the value to a domain not included in the URL;

Loose domain names cannot be set to tight domain names.

3. Image Ping

var img=new Image();img.onload=img.onerror=function(){... ...}img.src="url?name=value";

The request data is sent in the form of a query string. The response can be any content, usually a pixel chart or 204 response.

Image Ping is most commonly used to track the number of user clicks on a page or dynamic ad exposures.

Disadvantages:

Only GET requests can be sent;

The server response text cannot be accessed, and can only be used for one-way communication between the browser and the server.

4. Jsonp

var script=document.createElement("script");script.src="url?callback=handleResponse";document.body.insertBefore(script,document.body.firstChild);

JSONP consists of two parts: callback function and Data

The callback function is the function that should be called on the page when the response is received. Its name is generally specified in the request.

Data is the JSON data passed into the callback function.

Advantages:

It can directly access the response text and can be used for bidirectional communication between the browser and the server.

Disadvantages:

JSONP loads code execution from other domains, and other domains may be insecure;

It is difficult to determine whether the JSONP request fails.

5. Comet

Comet allows the server to push data to the browser.

Comet is the implementation method: Long polling and stream

Short polling means that the browser regularly sends requests to the server to check whether there is any data update.

Long polling means that the browser sends a request to the server, and the server keeps the connection open until data can be sent. After the data is sent, the browser closes the connection and then initiates a new request to the server. The advantage is that all browsers support XHR objects and setTimeout.

The browser sends a request to the server, while the server keeps the connection open and periodically sends data to the browser. Only one HTTP connection is used throughout the lifecycle of the page.

6. WebSocket

WebSocket can provide full-duplex and bidirectional communication on a single persistent connection.

WebSocket uses a custom protocol. When an unencrypted connection is established, ws: //. The encrypted link is wss ://.

var webSocket=new WebSocket("ws://");webSocket.send(message);webSocket.onmessage=function(event){var data=event.data;... ....}

Note:

The WebSocket constructor must be input with an absolute URL;

WebSocket can open the connection to any site. Whether or not it will communicate with the page in a domain depends entirely on the server;

WebSocket can only send plain text data. For complex data structures, JSON. stringify (message) must be serialized before sending )).

Advantages:

Sending a very small amount of data between the client and the server reduces the byte overhead.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.