Navigator.sendBeacon()

The navigator.sendBeacon() method asynchronously sends a small amount of data over HTTP to a web server.

Syntax

navigator.sendBeacon(url, data);

Parameters

url
The URL that will receive the data. Can be relative or absolute.
data
A ArrayBuffer, ArrayBufferView, Blob, DOMString, FormData, or URLSearchParams object containing the data to send.

Return values

The sendBeacon() method returns true if the user agent successfully queued the data for transfer. Otherwise, it returns false.

Description

This method is for analytics and diagnostics that send data to a server before the document is unloaded, where sending the data any sooner may miss some possible data collection. For example, which link the user clicked before navigating away and unloading the page.

Ensuring that data has been sent during the unloading of a document has traditionally been difficult, because user agents typically ignore asynchronous XMLHttpRequests made in an unload handler.

Historically, this was addressed with some of the following workarounds to delay the page unload long enough to send data to some URL:

  • Submitting the data with a blocking synchronous XMLHttpRequest call in unload or beforeunload event handlers.
  • Creating an <img> element and setting its src in the unload handler. Most user agents will delay the unload to load the image.
  • Creating a no-op loop for several seconds in the unload handler.

All of these methods block unloading the document, which slows down the next navigation. There is nothing the next page can do to avoid this, so the new page seems slow, even though it's the previous page's fault.

The following example shows theoretical analytics code that attempts to submit data to a server with a synchronous XMLHttpRequest in an unload handler. This results in the next page load to be delayed.

window.addEventListener("unload", function logData() {
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "/log", false); // third parameter of `false` means synchronous
  xhr.send(analyticsData); 
});

This is what sendBeacon() replaces. With the sendBeacon() method, the data is transmitted asynchronously when the User Agent has an opportunity to do so, without delaying unload or the next navigation. This solves all of the problems with submission of analytics data:

  • The data is sent reliably
  • It's sent asynchronously
  • It doesn't impact the loading of the next page
  • In addition, the code is simpler to write than any of the older techniques!

The following example shows a theoretical analytics code pattern that submits data to a server using the sendBeacon() method.

window.addEventListener("unload", function logData() {
  navigator.sendBeacon("/log", analyticsData);
});

The beacon sends an HTTP request via the POST method, with all relevant cookies available when called.

Specifications

Specification Status Comment
Beacon
The definition of 'sendBeacon()' in that specification.
Candidate Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
sendBeaconChrome Full support 39
Notes
Full support 39
Notes
Notes Starting in Chrome 59, this method cannot send a Blob whose type is not CORS safelisted. This is a temporary change until a mitigation can be found for the security issues that this creates. For more information see Chrome bug 720283.
Edge Full support 14Firefox Full support 31IE No support NoOpera Full support 26
Notes
Full support 26
Notes
Notes Starting in Opera 46, this method cannot send a Blob whose type is not CORS safelisted. This is a temporary change until a mitigation can be found for the security issues that this creates. For more information see Chrome bug 720283.
Safari Full support 11.1WebView Android Full support 40
Notes
Full support 40
Notes
Notes Starting in Chrome 59, this method cannot send a Blob whose type is not CORS safelisted. This is a temporary change until a mitigation can be found for the security issues that this creates. For more information see Chrome bug 720283.
Chrome Android Full support 42
Notes
Full support 42
Notes
Notes Starting in Chrome 59, this method cannot send a Blob whose type is not CORS safelisted. This is a temporary change until a mitigation can be found for the security issues that this creates. For more information see Chrome bug 720283.
Firefox Android Full support 31Opera Android Full support 26
Notes
Full support 26
Notes
Notes Starting in Opera 46, this method cannot send a Blob whose type is not CORS safelisted. This is a temporary change until a mitigation can be found for the security issues that this creates. For more information see Chrome bug 720283.
Safari iOS Full support 11.3Samsung Internet Android Full support 4.0
Notes
Full support 4.0
Notes
Notes Starting in Samsung Internet 7.0, this method cannot send a Blob whose type is not CORS safelisted. This is a temporary change until a mitigation can be found for the security issues that this creates. For more information see Chrome bug 720283.

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

See also