Using Navigator.sendbeacon () to escalate data

Source: Internet
Author: User

Http://kaifage.com/notes/76/navigator-sendBeacon.html

such as some statistical systems, when the page unload, if you want to report the current data, the use of xhr synchronous escalation, will block the current page jumps, using new image may encounter aborted, resulting in a failure to send successfully.
Now, you can use a browser to provide a more concise Sendbeacon method for sending guarantees. The Sendbeacon is asynchronous and does not affect the jump speed of the current page to the next page, and is not restricted by the same domain.

Window.addeventlistener (' Unload ', Logdata, false);

function Logdata () {
Navigator.sendbeacon ("/log", analyticsdata);
}

Sendbeacon returns True if it successfully enters the send queue of the browser, or False if the total number of queues, the size of the data, is limited. After returning ture, it simply means that the sending queue is entered, and the browser tries to ensure that it succeeds, but that it does not have any return value. Currently, there is no specific data length limit standard.

In view of the current browser support, you need to do a downgrade support (such as XHR in synchronous mode, if not the same domain to support Cors):

Navigator.sendbeacon | | New Function (' var xhr=new xmlhttprequest (); Xhr.open ("POST", arguments[0],true); R.send (arguments[1]); ');

Current browser support for Sendbeacon (see latest progress: http://caniuse.com/#search =sendbeacon):

Chrome 37+
Firefox (Gecko) 31+
Internet Explorer does not support
Opera 24+
Safari does not support
Phone-side Browser not supported: Android browser support, but iOS is not yet supported

Google Analytics has used Navigator.sendbeacon () to report data:
http://www.thyngster.com/google-analytics-added-sendbeacon-functionality-universal-analytics-javascript-api/
Https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=zh-cn

Via
W3 Standard Description: https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/Beacon/Overview.html
MDN Description: Https://developer.mozilla.org/en-US/docs/Web/API/navigator.sendBeacon

Using Navigator.sendbeacon () to escalate data

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.