navigator.sendBeacon() method asynchronously sends an HTTP POST request in reliable way, it's mostly used to send analytics.
it returns true if the user agent successfully queued the data for transfer. Otherwise, it returns false.
Unlike traditional HTTP requests, sendBeacon will always reach to the server even if you unload or exit the page in the middle of the request.
Example
document.addEventListener('visibilitychange', function logData() {
if (document.visibilityState === 'hidden') {
navigator.sendBeacon('/log', analyticsData);
}
});
Top comments (0)