--- url: "https://web.dev/articles/web-apps/share" titulek: "How to let the user share the website they are on | Web Apps | web.dev" h1: "How to let the user share the website they are on Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/web-apps/share" drobecky: ["Web Apps", "How to let the user share the website they are on"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:49:05" znaku: 7068 slov: 1004 kodovani: "iso-8859-2" sha256: "5bca7647fe7a792ec60b9c11ddc353c910b7a887e06c9e054cadb926cc7e9fd8" --- How to let the user share the website they are on Stay organized with collections Save and categorize content based on your preferences. Thomas Steiner Letting the user share the website they are on is a common web apps pattern that you can find on many news sites, blogs, or shopping sites. Since linking is one of the web's super powers, the hope is to acquire traffic from users who see the shared link on social networking sites, or who receive it via chat messages or even plain old school via email. The modern way The [Web Share API](/web-share) lets the user share data like the URL of the page they are on, along with a title and descriptive text. The `navigator.share()` method of the Web Share API invokes the native sharing mechanism of the device. It returns a promise and takes a single argument with the to-be-shared data. Possible values are: `url`: A string representing the URL to be shared. `text`: A string representing text to be shared. `title`: A string representing a title to be shared. May be ignored by the browser. Browser Support 128 93 12.1 [Source](https://developer.mozilla.org/docs/Web/API/Navigator/share) The classic way Not all browsers support the Web Share API yet. A fallback is thus to integrate with your target audience's most popular social networking sites. A popular example is Twitter, whose [Web Intent URL](https://developer.twitter.com/en/docs/twitter-for-websites/tweet-button/guides/web-intent) allows for a text and a URL to be shared. The method typically consists of crafting a URL and opening it in a browser. UI considerations It is a nice touch to respect the platform's established share icon according to the UI guidelines of the operating system vendors. Windows: Apple: Android and other operating systems: Progressive enhancement The snippet below uses the Web Share API when it is supported, then falls back to Twitter's Web Intent URL. `// DOM references const button = document.querySelector('button'); const icon = button.querySelector('.icon'); const canonical = document.querySelector('link[rel="canonical"]'); // Find out if the user is on a device made by Apple. const IS_MAC = /Mac|iPhone/.test(navigator.platform); // Find out if the user is on a Windows device. const IS_WINDOWS = /Win/.test(navigator.platform); // For Apple devices or Windows, use the platform-specific share icon. icon.classList.add(`share${IS_MAC? 'mac' : (IS_WINDOWS? 'windows' : '')}`); button.addEventListener('click', async () => { // Title and text are identical, since the title may actually be ignored. const title = document.title; const text = document.title; // Use the canonical URL, if it exists, else, the current location. const url = canonical?.href || location.href; // Feature detection to see if the Web Share API is supported. if ('share' in navigator) { try { await navigator.share({ url, text, title, }); return; } catch (err) { // If the user cancels, an `AbortError` is thrown. if (err.name !== "AbortError") { console.error(err.name, err.message); } } } // Fallback to use Twitter's Web Intent URL. // (https://developer.twitter.com/en/docs/twitter-for-websites/tweet-button/guides/web-intent) const shareURL = new URL('https://twitter.com/intent/tweet'); const params = new URLSearchParams(); params.append('text', text); params.append('url', url); shareURL.search = params; window.open(shareURL, '_blank', 'popup,noreferrer,noopener'); });` Further reading [Web Share API](/web-share) Demo HTML ` How to let the user share the website they are on

How to let the user share the website they are on

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at libero eget ante congue molestie. Integer varius enim leo. Duis est nisi, ullamcorper et posuere eu, mattis sed lorem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In at suscipit erat, et sollicitudin lorem.

In euismod ornare scelerisque. Nunc imperdiet augue ac porttitor porttitor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur eget pretium elit, et interdum quam.


` CSS `:root { color-scheme: dark light; } html { box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; } body { margin: 1rem; font-family: system-ui, sans-serif; } img, video { height: auto; max-width: 100%; } button { display: flex; } button .icon { display: inline-block; width: 1em; height: 1em; background-size: 1em; } @media (prefers-color-scheme: dark) { button .icon { filter: invert(); } } .share { background-image: url('share.svg'); } .sharemac { background-image: url('sharemac.svg'); }` JS `// DOM references const button = document.querySelector('button'); const icon = button.querySelector('.icon'); const canonical = document.querySelector('link[rel="canonical"]'); // Find out if the user is on a device made by Apple. const IS_MAC = /Mac|iPhone/.test(navigator.platform); // Find out if the user is on a Windows device. const IS_WINDOWS = /Win/.test(navigator.platform); // For Apple devices or Windows, use the platform-specific share icon. icon.classList.add(`share${IS_MAC? 'mac' : (IS_WINDOWS? 'windows' : '')}`); button.addEventListener('click', async () => { // Title and text are identical, since the title may actually be ignored. const title = document.title; const text = document.title; // Use the canonical URL, if it exists, else, the current location. const url = canonical?.href || location.href; // Feature detection to see if the Web Share API is supported. if ('share' in navigator) { try { await navigator.share({ url, text, title, }); return; } catch (err) { // If the user cancels, an `AbortError` is thrown. if (err.name !== "AbortError") { console.error(err.name, err.message); } } } // Fallback to use Twitter's Web Intent URL. // (https://developer.twitter.com/en/docs/twitter-for-websites/tweet-button/guides/web-intent) const shareURL = new URL('https://twitter.com/intent/tweet'); const params = new URLSearchParams(); params.append('text', text); params.append('url', url); shareURL.search = params; window.open(shareURL, '_blank', 'popup,noreferrer,noopener'); });` Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-09-04 UTC.