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