<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>How to let the user share the website they are on</title>
    <link rel="stylesheet" href="style.css" />
    <!-- TODO: Devsite - Removed inline handlers -->
    <!-- <script src="script.js" defer></script> -->
    <h1>How to let the user share the website they are on</h1>
      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.
    <img src="" width=400 height=300>
      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.
    <hr />
    <button type="button"><span class="icon"></span>Share</button>


        :root {
  color-scheme: dark light;

html {
  box-sizing: border-box;

*, *:before, *:after {
  box-sizing: inherit;

body {
  margin: 1rem;
  font-family: system-ui, sans-serif;

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');


        // 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({
    } catch (err) {
      // If the user cancels, an `AbortError` is thrown.
      if ( !== "AbortError") {
        console.error(, err.message);
  // Fallback to use Twitter's Web Intent URL.
  // (
  const shareURL = new URL('');
  const params = new URLSearchParams();
  params.append('text', text);
  params.append('url', url); = params;, '_blank', 'popup,noreferrer,noopener');