Beacon Sample

Available in Chrome 39+ | View on GitHub | Browse Samples

Background

Beacon lets you asynchronously transfer small HTTP data from the User Agent to a web server. It can be used e.g. to send analytics or diagnostics code without delaying the page's unload or affecting the performance of the next navigation.

The sample below sends a beacon to a sample server at https://putsreq.herokuapp.com/4GE2nVUuDoDGsNyKES2G on unload. Close this page and visit the PutsReq inspect page to see if the beacon data was received.

JavaScript Snippet

if ('sendBeacon' in navigator) {
window.addEventListener('pagehide', function() {
navigator.sendBeacon(
'https://putsreq.herokuapp.com/4GE2nVUuDoDGsNyKES2G',
'Sent by a beacon!');
}, false);
}