Code
Browser Web APIs
Allows web applications to redirect audio output to a permitted Bluetooth headset, speakerphone, or other device.
Provides a way for browsers to better manage audio sessions and inform browsers about what kind of audio is being played.
Provides a method for managing downloads that may take a significant amount of time, running in the background if the user navigates away.
Enables web apps to defer tasks to be run in a service worker once the user has a stable network connection.
Allows scheduling of non-critical work to be performed during idle periods via requestIdleCallback.
Allows installed web apps to set a badge on the app icon to notify users of pending activity such as messages or updates.
Detects linear and two-dimensional barcodes, including QR codes, in images and video streams.
Provides information about the battery charge level of the device and notifications when the level or status changes.
Sends asynchronous, non-blocking requests to a server without expecting a response, ideal for analytics and diagnostics.
Enables simple communication between browsing contexts such as tabs, windows, and iframes sharing the same origin.
Beacon API
Provides a fire-and-forget mechanism for sending small amounts of data to a server asynchronously and non-blockingly. Unlike fetch, beacon requests are guaranteed to be dispatched even when the page is unloading — making them ideal for analytics, diagnostics, and session tracking on page exit.
Key Method
navigator.sendBeacon(url, data?)— Queues a POST request. Returnstrueif queued successfully,falseif not (e.g. queue is full). No response is accessible.- Accepted data types:
ArrayBuffer,Blob,FormData,URLSearchParams, or a string.
Example — Send analytics on page unload
Use visibilitychange (preferred over unload) to reliably dispatch analytics when the user navigates away.
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
const payload = JSON.stringify({
page: location.pathname,
timeOnPage: Date.now() - pageLoadTime,
scrollDepth: getScrollDepth(),
});
const blob = new Blob([payload], { type: 'application/json' });
navigator.sendBeacon('/api/analytics', blob);
}
});Example — Send form data as a beacon
Use FormData to send structured data without needing to JSON-encode it.
const data = new FormData();
data.append('event', 'button_click');
data.append('target', 'cta-signup');
data.append('timestamp', Date.now());
const queued = navigator.sendBeacon('/api/events', data);
console.log('Beacon queued:', queued); // true or falseExample — Send URL-encoded params
const params = new URLSearchParams({
uid: 'user_42',
action: 'logout',
session: sessionStorage.getItem('session_id'),
});
navigator.sendBeacon('/api/session', params);Beacon vs. Fetch on unload
| Beacon | Fetch (keepalive) | |
|---|---|---|
| Works on page unload | Yes | Yes (with keepalive: true) |
| Response accessible | No | Yes |
| Method | POST only | Any |
| Body size limit | ~64 KB | 64 KB |