Web Share API
The Web Share API lets a web page open the operating system’s share sheet, the same panel a native app uses to send a link to a messaging app, an email or a note. navigator.share() takes the data to share and returns a promise, which is fulfilled when the person has shared it and rejected when they cancel or the share fails.
It suits pages people pass on, such as articles, recipes or event details, because the share sheet already lists the apps that person uses, and the page needs no buttons for each service.
Requirements
Section titled “Requirements”- The page must be served over HTTPS, or from
localhostwhile developing. navigator.share()must be called in response to a user action, such as a click. Called at any other time, it is rejected with aNotAllowedError.- Inside an
<iframe>, the frame needsallow="web-share". - Support varies. Safari and Chromium-based browsers support it on most platforms, while Firefox supports it only on Android. The MDN page for navigator.share() has the current list, so the code should check for the method before offering it.
Sharing a link
Section titled “Sharing a link”The data object can hold a title, some text and a url, and at least one of them must be given. This page shows a share button only where the browser supports sharing:
<h1>Lemon drizzle cake</h1><button type="button" id="share-recipe" hidden>Share this recipe</button><p id="share-status"></p><script src="share.js"></script>const button = document.querySelector('#share-recipe');const status = document.querySelector('#share-status');
const recipe = { title: 'Lemon drizzle cake', text: 'A lemon drizzle cake that takes ten minutes to mix.', url: location.href,};
if ('share' in navigator) { button.hidden = false;} else { status.textContent = 'Copy the address bar to share this recipe.';}
button.addEventListener('click', () => { navigator .share(recipe) .then(() => { status.textContent = 'Thanks for sharing.'; }) .catch((error) => { if (error.name === 'AbortError') { status.textContent = 'Sharing was cancelled.'; } else { status.textContent = `Could not share: ${error.message}`; } });});In a supporting browser, the button appears, and clicking it opens the share sheet. Choosing an app fulfils the promise and the paragraph thanks the reader, while closing the sheet rejects it with an AbortError and the paragraph says sharing was cancelled. In a browser without the API, the button stays hidden and the paragraph suggests copying the address instead.
Cancelling counts as a rejection, so a catch() that treats every rejection as a fault would report an error each time someone changes their mind. Checking error.name separates the two cases.
Sharing files
Section titled “Sharing files”Some browsers can share files too, such as an image or a text file. Not every browser or file type is supported, so navigator.canShare() tests the data first and returns true only when it can be shared:
<button type="button" id="share-list">Share the shopping list</button><script src="share-file.js"></script>const shareList = (items) => { const file = new File([items.join('\n')], 'shopping-list.txt', { type: 'text/plain' }); const data = { files: [file], title: 'Shopping list' };
if (!navigator.canShare?.(data)) { return Promise.reject(new Error('This browser cannot share files')); } return navigator.share(data);};
document.querySelector('#share-list').addEventListener('click', () => { shareList(['bread', 'milk', 'apples']).catch((error) => console.log(error.message));});Clicking the button opens the share sheet with a file called shopping-list.txt, which holds one item per line. Where the browser cannot share files, the promise is rejected and the console shows “This browser cannot share files”. The optional chaining in navigator.canShare?.(data) covers browsers that lack canShare() altogether.
Steve Griffith covers this topic in his video Web Share API.