navigator.sendBeacon()
方法
asynchronously
sends a small amount of data over
HTTP
to a web server.
navigator.sendBeacon(url, data);
url
data
ArrayBuffer
,
ArrayBufferView
,
Blob
,
DOMString
,
FormData
,或
URLSearchParams
object containing the data to send.
sendBeacon()
方法返回
true
若
用户代理
successfully queued the
data
for transfer. Otherwise, it returns
false
.
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
XMLHttpRequest
s 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:
XMLHttpRequest
call in
unload
or
beforeunload
event handlers.
<img>
element and setting its
src
在
unload
handler. Most user agents will delay the unload to load the image.
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 following example shows a theoretical analytics code pattern that submits data to a server using the
sendBeacon()
方法。
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.
| 规范 | 状态 | 注释 |
|---|---|---|
|
信标
The definition of 'sendBeacon()' in that specification. |
候选推荐 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
sendBeacon
|
Chrome
39
|
Edge 14 | Firefox 31 | IE No |
Opera
26
|
Safari 11.1 |
WebView Android
40
|
Chrome Android
42
|
Firefox Android 31 |
Opera Android
26
|
Safari iOS 11.3 |
Samsung Internet Android
4.0
|
完整支持
不支持
见实现注意事项。
Navigator
activeVRDisplays
appCodeName
appName
appVersion
battery
buildID
clipboard
connection
cookieEnabled
credentials
deviceMemory
doNotTrack
geolocation
keyboard
语言
languages
locks
maxTouchPoints
mediaDevices
mediaSession
onLine
oscpu
permissions
platform
product
productSub
serviceWorker
userAgent
vendor
vendorSub
webdriver
xr
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers