Web Share API

W3C Recommendation

More details about this document
This version:
https://www.downtownmelody.com/_x/d3d3LnczLm9yZw/TR/2023/REC-web-share-20230530/
Latest published version:
https://www.downtownmelody.com/_x/d3d3LnczLm9yZw/TR/web-share/
Latest editor's draft:
https://www.downtownmelody.com/_x/dzNjLmdpdGh1Yi5pbw/web-share/
History:
https://www.downtownmelody.com/_x/d3d3LnczLm9yZw/standards/history/web-share
Commit history
Test suite:
https://www.downtownmelody.com/_x/d3B0LmxpdmU/web-share/
Implementation report:
https://www.downtownmelody.com/_x/dzNjLmdpdGh1Yi5pbw/web-share/imp-report/
Editors:
Matt Giuca (Google Inc.)
Eric Willigers (Google Inc.)
Marcos Cáceres (Apple Inc.)
Feedback:
GitHub w3c/web-share (pull requests, new issue, open issues)
Errata:
Errata exists.
Browser support:
caniuse.com

See also translations.


Abstract

This specification defines an API for sharing text, links and other content to an arbitrary destination of the user's choice.

The available share targets are not specified here; they are provided by the user agent. They could, for example, be apps, websites or contacts.

Status of This Document

This section describes the status of this document at the time of its publication. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.downtownmelody.com/_x/d3d3LnczLm9yZw/TR/.

This document was published by the Web Applications Working Group as a Recommendation using the Recommendation track.

W3C recommends the wide deployment of this specification as a standard for the Web.

A W3C Recommendation is a specification that, after extensive consensus-building, is endorsed by W3C and its Members, and has commitments from Working Group members to royalty-free licensing for implementations. Future updates to this Recommendation may incorporate new features.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 2 November 2021 W3C Process Document.

Implementation status

This section is non-normative.

The ability to share content is often dependent on the underlying operating system providing a "share" capability and also on OS UI conventions. For example, some OSs present a "share sheet", while others rely on an pop-up menu. Due to these aforementioned dependencies, there is ongoing work by implementers to bring the Web Share API to all OSs. This ongoing effort is reflected as failures in the implementation report, which is generated by running tests on a limited set of OSs. However, the Working Group is optimistic that the Web Share API will become more available across all OSs over time, and is already widely available on popular OSs across a range of devices.

1. Usage Examples

This section is non-normative.

1.2 Sharing a file

This example shows how to share a file. Note that the files member is an array, allowing for multiple files to be shared.

Example 2: Sharing a file
shareButton.addEventListener("click", async () => {
  const file = new File(data, "some.png", { type: "image/png" });
  try {
    await navigator.share({
      title: "Example File",
      files: [file]
    });
  } catch (err) {
    console.error("Share failed:", err.message);
  }
});

1.3 Validating a share

Calling canShare() method with a ShareData dictionary validates the shared data. Unlike share(), it can be called without transient activation.

const file = new File([], "some.png", { type: "image/png" });

// Check if files are supported
if (navigates.canShare({files: [file]})) {
  // Sharing a png file would probably be ok...
}

// Check if a URL is ok to share...
if (navigates.canShare({ url: someURL })) {
  // The URL is valid and can probably be shared...
}

1.4 Checking if members are supported

Because of how WebIDL dictionaries work, members passed to share() that are unknown to the user agent are ignored. This can be a problem when sharing multiple members, but the user agent doesn't support sharing one of those members. To be sure that every member being passed is supported by the user agent, you can pass them to canShare() individually to check if they are supported.

Example 4: Future-proofing shares
const data = {
  title: "Example Page",
  url: "https://example.com",
  text: "This is a text to share",
  someFutureThing: "some future thing",
};
const allSupported = Object.entries(data).every(([key, value]) => {
  return navigator.canShare({ [key]: value });
});
if (allSupported) {
  await navigator.share(data);
}

Alternatively, you can adjust application's UI to not show UI components for unsupported members.

Example 5: Filtering out unsupported members
const data = {
  title: "Example Page",
  url: "https://example.com",
  text: "This is a text to share",
  someFutureThing: "some future thing",
};

// Things that are not supported...
const unsupported = Object.entries(data).filter(([key, value]) => {
  return !navigator.canShare({ [key]: value });
});

1.5 Enabling the API in third-party contexts

The default allowlist of 'self' makes Web Share API available by default only in first-party contexts.

Third-party can be allowed to use this API via an iframe's allow attribute:

Alternatively, the API can be disabled in a first-party context by specifying an HTTP response header:

See the Permissions Policy specification for more details and for how to control the permission policies on a per-origin basis.

2. API definition

2.1 Extensions to the Navigator interface

WebIDLpartial interface Navigator {
  [SecureContext] Promise<undefined> share(optional ShareData data = {});
  [SecureContext] boolean canShare(optional ShareData data = {});
};

2.1.1 Internal Slots

This API adds the following internal slot to the Navigator interface.

Promise? [[sharePromise]]
The this.[[sharePromise]] is a promise that represents a user's current intent to share some data with a