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

句法

navigator.sendBeacon(url, data);
					

参数

url
The URL that will receive the data . Can be relative or absolute.
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:

  • Submitting the data with a blocking synchronous XMLHttpRequest call in unload or beforeunload event handlers.
  • Creating an <img> element and setting its src 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() 方法。

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.
候选推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
sendBeacon Chrome 39
39
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 14 Firefox 31 IE No Opera 26
26
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 11.1 WebView Android 40
40
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 42
42
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 31 Opera Android 26
26
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 11.3 Samsung Internet Android 4.0
4.0
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 .

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. Navigator
  2. 特性
    1. activeVRDisplays
    2. appCodeName
    3. appName
    4. appVersion
    5. battery
    6. buildID
    7. clipboard
    8. connection
    9. cookieEnabled
    10. credentials
    11. deviceMemory
    12. doNotTrack
    13. geolocation
    14. keyboard
    15. 语言
    16. languages
    17. locks
    18. maxTouchPoints
    19. mediaDevices
    20. mediaSession
    21. onLine
    22. oscpu
    23. permissions
    24. platform
    25. product
    26. productSub
    27. serviceWorker
    28. userAgent
    29. vendor
    30. vendorSub
    31. webdriver
    32. xr
  3. 方法
    1. canShare()
    2. getBattery()
    3. getGamepads()
    4. getUserMedia()
    5. getVRDisplays()
    6. mozIsLocallyAvailable()
    7. msLaunchUri()
    8. registerContentHandler()
    9. registerProtocolHandler()
    10. requestMediaKeySystemAccess()
    11. sendBeacon()
    12. share()
    13. taintEnabled()
    14. vibrate()
  4. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMediaElement
    42. HTMLMetaElement
    43. HTMLMeterElement
    44. HTMLModElement
    45. HTMLOListElement
    46. HTMLObjectElement
    47. HTMLOptGroupElement
    48. HTMLOptionElement
    49. HTMLOptionsCollection
    50. HTMLOutputElement
    51. HTMLParagraphElement
    52. HTMLParamElement
    53. HTMLPictureElement
    54. HTMLPreElement
    55. HTMLProgressElement
    56. HTMLQuoteElement
    57. HTMLScriptElement
    58. HTMLSelectElement
    59. HTMLShadowElement
    60. HTMLSourceElement
    61. HTMLSpanElement
    62. HTMLStyleElement
    63. HTMLTableCaptionElement
    64. HTMLTableCellElement
    65. HTMLTableColElement
    66. HTMLTableDataCellElement
    67. HTMLTableElement
    68. HTMLTableHeaderCellElement
    69. HTMLTableRowElement
    70. HTMLTableSectionElement
    71. HTMLTemplateElement
    72. HTMLTextAreaElement
    73. HTMLTimeElement
    74. HTMLTitleElement
    75. HTMLTrackElement
    76. HTMLUListElement
    77. HTMLUnknownElement
    78. HTMLVideoElement
    79. HashChangeEvent
    80. 历史
    81. ImageData
    82. 定位
    83. MessageChannel
    84. MessageEvent
    85. MessagePort
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1