JavaScript Navigator share() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

navigator.share(data) opens the device’s native share UI so the user can send title, text, a url, and/or files (MDN). It returns a Promise that resolves with undefined.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call navigator.share. Start with this line.

JavaScript
navigator.share(data?)
CallWhat it does
navigator.share(data?)Primary signature.
navigator.share(...)Call with the arguments your case needs.

Built into the browser. Call share(...) on a Navigator — no import required.

Library declarations
JavaScript
navigator.share(data?)

Parameters

ParameterDescription
dataOptional. Share payload (*at least one known property)
url / text / titleOptional. Strings; empty url means current page (MDN)
filesOptional. File[] — test with canShare first

Return Value

Return ValueDescription
*Promise → undefined

JavaScript Examples

Example 1 — Share a URL (MDN)

Call share from a button click with title, text, and url.

JavaScript
const shareData = {
  title: "MDN",
  text: "Learn web development on MDN!",
  url: "https://developer.mozilla.org"
};

document.querySelector("button").addEventListener("click", async () => {
  try {
    await navigator.share(shareData);
    console.log("MDN shared successfully");
  } catch (err) {
    console.log("Error: " + err);
  }
});
Try It Yourself

How It Works

  1. Build ShareData with known fields the UA understands.
  2. Wire a click — share needs transient activation (MDN).
  3. Await the Promise — resolves with undefined on success.
  4. Catch errors for cancel, missing support, or invalid data.

Example 2 — Validate with canShare, Then Share

MDN file/link pattern: check first, then open the sheet from the same gesture.

JavaScript
const data = {
  title: "CodeToFun",
  text: "JS tutorials",
  url: location.href
};

document.querySelector("button").addEventListener("click", async () => {
  if (typeof navigator.share !== "function") {
    console.log("Web Share not supported");
    return;
  }
  if (navigator.canShare && !navigator.canShare(data)) {
    console.log("Cannot share this data");
    return;
  }
  await navigator.share(data);
  console.log("Shared!");
});
Try It Yourself

How It Works

  1. Detect share before calling it.
  2. Optional canShare avoids opening a sheet that will fail.
  3. Same click handler keeps the user gesture valid.
  4. Especially for files — MDN recommends testing with canShare({ files }).

Example 3 — Treat AbortError as Cancel

User dismissals are normal — do not show them as hard failures.

JavaScript
document.querySelector("button").addEventListener("click", async () => {
  if (!navigator.share) {
    console.log("unsupported");
    return;
  }
  try {
    await navigator.share({
      title: "Hello",
      url: location.href
    });
    console.log("ok");
  } catch (err) {
    if (err.name === "AbortError") {
      console.log("cancelled");
    } else {
      console.log(err.name + ": " + err.message);
    }
  }
});
Try It Yourself

How It Works

  1. Wrap in try/catch — share rejects on cancel and many failures.
  2. AbortError means the user closed the sheet or no targets (MDN).
  3. Other names (NotAllowedError, TypeError) need a fallback message.
  4. Keep UX calm — cancel is not an app bug.

Browser Support

navigator.share() is part of the Web Share API and is not Baseline. Strong on many mobile browsers and modern Chromium / Safari; desktop support varies. Always feature-detect, require HTTPS, and keep a copy-link fallback.

✓ Limited availability · Not Baseline

Navigator.share()

Native share sheet for title, text, url, and files — Promise<undefined>.

Limited Not Baseline
Google Chrome Supported (platform-dependent)
Limited
Microsoft Edge Follow Chromium Web Share support
Limited
Apple Safari Strong on iOS / macOS Safari
Yes
Opera Follow Chromium Web Share support
Limited
Mozilla Firefox Often limited / flag-gated — feature-detect
Limited
Internet Explorer No Web Share support
No
share() Limited

Bottom line: Detect share, validate with canShare when needed, call only from a click, treat AbortError as cancel. See the MDN share() reference.

Frequently Asked Questions

It invokes the device’s native sharing mechanism so the user can send title, text, a URL, and/or files to targets such as Messages, Mail, or social apps (MDN). It returns a Promise that resolves with undefined.
No. MDN does not mark it Deprecated, Experimental, or Non-standard. It is Limited availability (not Baseline) and requires a secure context. Always feature-detect.
The API requires transient activation (a recent user gesture). Scripts cannot open the share sheet at arbitrary times (MDN).
navigator.canShare(data) returns true if share(data) should succeed. Use it especially before sharing files, then call share from the same click handler.
The Promise is typically rejected with AbortError. Treat cancel as a normal outcome, not a hard failure.
Yes. The Web Share API is a secure-context feature (HTTPS or localhost) in supporting browsers (MDN).

Next: taintEnabled()

Continue with the next Navigator method in the tutorial series.

taintEnabled() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

21 people found this page helpful