Send cross-domain requests with IMG and script

Source: Internet
Author: User

IMG.SRC can be used to send HTTP requests, but sending HTTP requests is not the real intention of img.src.

Similarly, using SCRIPT.SRC to request interface data in the JSONP format is not the original design purpose of the script element.

However, these 歪门邪道 techniques take advantage of the feature that DOM elements such as img/script can send cross-domain requests.

(New Image ()). src= "URL containing user behavior data"

Also write a IMG.SRC and 204 response combination of a scenario: the implementation of DNS pre-resolution and the establishment of TCP links!

Youtobe's front-end engineers say they use IMG.SRC requests to implement preloading video connection.

The implementation is in the head section of the HTML with the following script code:

var img = new Image();img.src = videoConnectionUrl;

The result of this is:

  • Resolves DNS while page are rendering, before it is needed
  • Maintains an open connection for later use

Look, it 剑走偏锋 's another use! But it is more powerful than regular dns-prefetch, and there are no issues that are not supported by a low-version browser.

Content Source: http://hzxiaosheng.bitbucket.org/work/2014/04/09/img-onerror-event-triggered-by-204-response/

Send cross-domain requests with IMG and script

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.