JavaScript Reference ยท Browser and Web APIs
The Web Share API
Reference for the Web Share API: navigator.share() and navigator.canShare(), share data fields, user activation rules, errors and a copy-link fallback.
The Web Share API lets a page hand a title, text, URL or files to the operating system's own share sheet. The user then picks the target (a messaging app, mail, a nearby device) from the same menu they see in native apps, and your page never needs to know which apps are installed.
Two methods make up the API: navigator.share(data) opens the sheet and returns a promise, and navigator.canShare(data) tells you in advance whether a given payload could be shared.
if (navigator.share) { await navigator.share({ title: "Article", text: "Worth a read", url: location.href }); }
Try it
Sharing needs a secure context, a click (user activation) and a browser or device that offers a share target. A sandboxed preview usually blocks it, so this example tries the real call, reports exactly what happened, and falls back to showing the link instead.
Syntax
navigator.share(data) navigator.canShare(data)
| Name | Description |
|---|---|
data.title |
Optional string. A title, which the target app may ignore. |
data.text |
Optional string. The body text. |
data.url |
Optional string. A URL, resolved against the page's URL. |
data.files |
Optional array of File objects. Only certain types are allowed, and support is narrower than for text and URLs. |
Return value: share() returns a promise that resolves with undefined once the user has completed the share, and canShare() returns a boolean.
Exceptions (as promise rejections for share()):
NotAllowedError: there was no user activation, a permissions policy forbids sharing (common in embedded frames), or the browser refused the files.AbortError: the user dismissed the share sheet.TypeError: the data is empty, or contains a URL that cannot be parsed.
Usage notes
- Call it from a click or tap handler. Calling
share()on page load, or after a longawait, can lose the user activation and fail. - Only one share at a time. Calling
share()while a sheet is open rejects. - Always feature-detect. Desktop browsers vary widely, so use
if (navigator.share)and offer a fallback such as a copy-link button or ordinarymailto:links. canShare()is mostly for files. For plain text and URLs, checking thatnavigator.shareexists is usually enough.- You do not learn which app was chosen. A resolved promise means the share went ahead, not where it went.
- Share a canonical URL. Do not include tracking parameters the reader would not want to pass on.
- Secure contexts only. The API is available on HTTPS pages (and localhost).
- Treat
AbortErroras normal. It is not a bug, so do not show an error message for it.
Browser support
Support is solid on phones and tablets and uneven on desktop, where it depends on the browser and operating system. Check a compatibility table, and keep a fallback.