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 specification was published by the Web Incubator Community Group. It is not a W3C Standard nor is it on the W3C Standards Track. Please note that under the W3C Community Contributor License Agreement (CLA) there is a limited opt-out and other conditions apply. Learn more about W3C Community and Business Groups.

This is an early draft of the Web Share spec.

1. Usage Examples

This section is non-normative.

This example shows a basic share operation. In response to a button click, this JavaScript code shares the current page's URL.

Example 1

: Basic usage

shareButton.addEventListener('click', () => {
  navigator.share({title: 'Example Page', url: ''})
      .then(console.log('Share successful'));
});

Note that a url of '' refers to the current page URL, just as it would in a link. Any other absolute or relative URL can also be used.

In response to this call to navigator.share, the user agent would display a picker or chooser dialog, allowing the user to select a target to share this title and the page URL to.

2. API definition

2.2 ShareData dictionary

dictionary ShareData {
    USVString title;
    USVString text;
    USVString url;
};

The ShareData dictionary consists of several optional fields:

Note
These fields are USVString (as opposed to DOMString) because they must not contain invalid UTF-16 surrogates. This means the user agent is free to re-encode them in any Unicode encoding (e.g., UTF-8).
Note
The url field may contain a relative URL. In this case, it will be automatically resolved relative to the current page location, just like a href on an a element, before being given to the share target.

3. Share targets

A share target is the abstract concept of a destination that the user agent will transmit the share data to. What constitutes a share target is at the discretion of the user agent.

A share target may not be directly able to accept a ShareData (due to not having been written with this API in mind). However, it MUST have the ability to receive data that matches some or all of the concepts exposed in ShareData. To convert data to a format suitable for ingestion into the target, the user agent SHOULD map the fields of ShareData onto equivalent concepts in the target. It MAY discard fields if necessary. The meaning of each field of the payload is at the discretion of the share target.

Each share target may be made conditionally available depending on the ShareData payload delivered to the share method.

Note
Once a share target has been given the payload, the share is considered successful. If the target considers the data unacceptable or an error occurs, the target should either recover gracefully, or show an error message to the end-user, because the sender is not going to know that an error occurred. In other words, the share method is "fire and forget"; it does not wait for the target to approve or reject the payload.

3.1 Examples of share targets

This section is non-normative.

The list of share targets can be populated from a variety of sources, depending on the user agent and host operating system. For example:

Note
There is an attempt to standardize the registration of websites to receive share data for that final use case; see Web Share Target.

In some cases, the host operating system will provide a sharing or intent system similar to Web Share. In these cases, the user agent may simply forward the share data to the operating system and not talk directly to native applications.

Mapping the ShareData to the share target (or operating system)'s native format can be tricky as some platforms will not have an equivalent set of fields. For example, if the target has a "text" field but not a "URL" field, it may be necessary to concatenate both the text and url fields of ShareData and pass the result in the "text" field of the target.

4. Security and privacy considerations

This section is non-normative.

Implementations should observe the following security and privacy advice.

Web Share enables data to be sent from websites to native applications. While this ability is not unique to Web Share, it does come with a number of potential security issues that may vary in severity (depending on the underlying platform).

A. Acknowledgments

Thanks to the Web Intents team, who laid the groundwork for the web app interoperability use cases. In particular, Paul Kinlan, who did a lot of early advocacy for Web Share.

B. References

B.1 Normative references

[HTML]
HTML Standard. Anne van Kesteren; Domenic Denicola; Ian Hickson; Philip Jägenstedt; Simon Pieters. WHATWG. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[URL]
URL Standard. Anne van Kesteren. WHATWG. Living Standard. URL: https://url.spec.whatwg.org/
[WEBIDL]
Web IDL. Cameron McCormack; Boris Zbarsky; Tobie Langel. W3C. 15 December 2016. W3C Editor's Draft. URL: https://heycam.github.io/webidl/

B.2 Informative references

[WEBIDL-LS]
Web IDL. Cameron McCormack; Boris Zbarsky; Tobie Langel. W3C. 15 December 2016. W3C Editor's Draft. URL: https://heycam.github.io/webidl/
[rfc2781]
UTF-16, an encoding of ISO 10646. P. Hoffman; F. Yergeau. IETF. February 2000. Informational. URL: https://tools.ietf.org/html/rfc2781
[rfc3629]
UTF-8, a transformation format of ISO 10646. F. Yergeau. IETF. November 2003. Internet Standard. URL: https://tools.ietf.org/html/rfc3629