安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

navigator.share() method of the Web Share API invokes the native sharing mechanism of the device.

句法

const sharePromise = navigator.share(data);
					

参数

data

An object containing data to share. At least one of the following fields must be specified. Available options are:

  • url : A USVString representing a URL to be shared.
  • text : A USVString representing text to be shared.
  • title : A USVString representing the title to be shared.
  • 文件 : A "FrozenArray" representing the array of file to be shared.

返回值

A Promise that will be fulfilled once a user has completed a share action (usually the user has chosen an application to share to). It will reject immediately if the data parameter is not correctly specified, and will also reject if the user cancels sharing.

范例

In our Web share test ( see the source code ) there is a button which, when clicked, invokes the Web Share API to share MDN's URL. The JavaScript looks like this:

const shareData = {
  title: 'MDN',
  text: 'Learn web development on MDN!',
  url: 'https://developer.mozilla.org',
}
const btn = document.querySelector('button');
const resultPara = document.querySelector('.result');
// Must be triggered some kind of "user activation"
btn.addEventListener('click', async () => {
  try {
    await navigator.share(shareData)
    resultPara.textContent = 'MDN shared successfully'
  } catch(err) {
    resultPara.textContent = 'Error: ' + err
  }
});
					

Sharing Files

To share files, first test for and call navigator.canShare() . Then include an array of files in the call to navigator.share():

Notice: That the sample handles feature detection by testing for navigator.canShare() rather than for navigator.share() . The data object passed to canShare() only supports the 文件 property. Image, video, audio, and text files can be shared.

if (navigator.canShare && navigator.canShare({ files: filesArray })) {
  navigator.share({
    files: filesArray,
    title: 'Pictures',
    text: 'Our Pictures.',
  })
  .then(() => console.log('Share was successful.'))
  .catch((error) => console.log('Sharing failed', error));
} else {
  console.log(`Your system doesn't support sharing files.`);
}
					

规范

规范 状态 注释
Web Share API
The definition of 'share()' 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
share Chrome No Edge No Firefox No IE No Opera No Safari 12.1 WebView Android No Chrome Android 61 Firefox Android No Opera Android 48 Safari iOS 12.2 Samsung Internet Android 8.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改:
  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