Web Share API
The Web Share API provides a mechanism for sharing text, links, files, and other content to an arbitrary share target selected by the user.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This API is not available in Web Workers (not exposed via WorkerNavigator
).
Note: This API should not be confused with the Web Share Target API, which allows a website to specify itself as a share target.
Concepts and usage
The Web Share API allows a site to share text, links, files, and other content to user-selected share targets, utilizing the sharing mechanisms of the underlying operating system. These share targets typically include the system clipboard, email, contacts or messaging applications, and Bluetooth or WiFi channels.
The API has just two methods. The navigator.canShare()
method may be used to first validate whether some data is "sharable", prior to passing it to navigator.share()
for sending.
The navigator.share()
method invokes the native sharing mechanism of the underlying operating system and passes the specified data. It requires transient activation, and hence must be triggered off a UI event like a button click. Further, the method must specify valid data that is supported for sharing by the native implementation.
The Web Share API is gated by the web-share permission policy. If the policy is supported but has not been granted, both methods will indicate that the data is not sharable.
Interfaces
navigator.canShare()
-
Returns a boolean indicating whether the specified data is sharable.
navigator.share()
-
Returns a
Promise
that resolves if the passed data was successfully sent to a share target. This method must be called on a button click or other user activation (requires transient activation).
Example
The code below shows how you can share a link using navigator.share()
, triggered off a button click.
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'); // Share must be triggered by "user activation" btn.addEventListener('click', async () => { try { await navigator.share(shareData) resultPara.textContent = 'MDN shared successfully' } catch(err) { resultPara.textContent = 'Error: ' + err } });
The above example is taken from our Web share test (see the source code). You can also see this as a live example in navigator.share()
.
Specifications
Specification |
---|
Web Share API # share-method |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Web_Share_API |
89
Not supported on macOS, see bug 1144920.
|
89
Not supported on macOS, see bug 1144920.
|
No |
No |
75
Not supported on macOS, see bug 1144920.
|
14 |
No |
75 |
No |
54 |
14 |
11.0 |
data_files_parameter |
89
Not supported on macOS, see bug 1144920.
|
89
Not supported on macOS, see bug 1144920.
|
No |
No |
75
Not supported on macOS, see bug 1144920.
|
15 |
No |
76 |
No |
54 |
15 |
11.0 |
data_text_parameter |
89
Not supported on macOS, see bug 1144920.
|
89
Not supported on macOS, see bug 1144920.
|
No |
No |
75
Not supported on macOS, see bug 1144920.
|
15 |
No |
76 |
No |
54 |
15 |
11.0 |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Web_Share_API |
89
Not supported on macOS, see bug 1144920.
|
81 |
71 |
No |
75 |
12.1 |
No |
61 |
79 |
48 |
12.2 |
8.0 |
data_files_parameter |
89
Not supported on macOS, see bug 1144920.
|
89
Not supported on macOS, see bug 1144920.
|
No |
No |
75
Not supported on macOS, see bug 1144920.
|
15 |
No |
76 |
No |
54 |
15 |
11.0 |
data_text_parameter |
89
Not supported on macOS, see bug 1144920.
|
89
Not supported on macOS, see bug 1144920.
|
No |
No |
75
Not supported on macOS, see bug 1144920.
|
15 |
No |
76 |
No |
54 |
15 |
11.0 |
BCD tables only load in the browser
See also
- Receiving shared data with the Web Share Target API (https://web.dev/)
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Web_Share_API