इस कुकी का इस्तेमाल, उपयोगकर्ताओं को उस वेबसाइट को शेयर करने में मदद करने के लिए किया जाता है जिस पर वे मौजूद हैं

उपयोगकर्ता को अपनी वेबसाइट शेयर करने की अनुमति देना, वेब ऐप्लिकेशन का एक सामान्य पैटर्न है. यह पैटर्न, आपको कई खबरों वाली साइटों, ब्लॉग या शॉपिंग साइटों पर दिख सकता है. लिंक करना, वेब की सबसे अहम सुविधाओं में से एक है. इसलिए, उम्मीद है कि सोशल नेटवर्किंग साइटों पर शेयर किया गया लिंक देखने वाले या चैट मैसेज या ईमेल के ज़रिए लिंक पाने वाले उपयोगकर्ताओं से ट्रैफ़िक मिलेगा.

Web Share API का इस्तेमाल करना

Web Share API की मदद से, उपयोगकर्ता उस पेज का यूआरएल शेयर कर सकता है जिस पर वह मौजूद है. इसके साथ ही, वह टाइटल और जानकारी देने वाला टेक्स्ट भी शेयर कर सकता है. Web Share API का navigator.share() तरीका, डिवाइस के शेयरिंग मैकेनिज़्म को शुरू करता है. यह एक प्रॉमिस दिखाता है और शेयर किए जाने वाले डेटा के साथ एक ही आर्ग्युमेंट लेता है. इसकी ये वैल्यू हो सकती हैं:

  • url: यह शेयर किए जाने वाले यूआरएल को दिखाने वाली स्ट्रिंग होती है.
  • text: यह शेयर किए जाने वाले टेक्स्ट को दिखाने वाली स्ट्रिंग होती है.
  • title: यह शेयर किए जाने वाले टाइटल को दिखाने वाली स्ट्रिंग होती है. ऐसा हो सकता है कि ब्राउज़र इसे अनदेखा कर दे.

Browser Support

  • Chrome: 128.
  • Edge: 93.
  • Firefox: behind a flag.
  • Safari: 12.1.

Source

सोशल नेटवर्किंग साइट के शेयर इंटेंट का इस्तेमाल करना

फ़िलहाल, सभी ब्राउज़र Web Share API को सपोर्ट नहीं करते हैं. इसलिए, फ़ॉलबैक के तौर पर, अपने टारगेट ऑडियंस की सबसे लोकप्रिय सोशल नेटवर्किंग साइटों के साथ इंटिग्रेट करें. Twitter इसका एक लोकप्रिय उदाहरण है. इसके Web Intent यूआरएल की मदद से, टेक्स्ट और यूआरएल शेयर किया जा सकता है. आम तौर पर, इस तरीके में एक यूआरएल बनाना और उसे ब्राउज़र में खोलना शामिल होता है.

यूज़र इंटरफ़ेस (यूआई) से जुड़ी बातें

ऑपरेटिंग सिस्टम के वेंडर की यूज़र इंटरफ़ेस (यूआई) से जुड़ी गाइडलाइन के मुताबिक, प्लैटफ़ॉर्म के तय किए गए शेयर आइकॉन का इस्तेमाल करना सबसे सही तरीका है.

    Windows का आइकॉन

    Apple का आइकॉन

    Android और अन्य ऑपरेटिंग सिस्टम

परतदार वृद्धि

स्निपेट, Web Share API का इस्तेमाल तब करता है, जब यह काम करता है. इसके बाद, यह Twitter के Web Intent यूआरएल पर फ़ॉलबैक करता है.

// 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 isMac = navigator.platform.toLowerCase().includes('mac');

// Find out if the user is on a Windows device.
const isWin = navigator.platform.toLowerCase().includes('win');

// For Apple devices or Windows, use the platform-specific share icon.
icon.classList.add(`share${isMac? 'mac' : (isWin? '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.
  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');
});

डेमो

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <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> -->
  </head>
  <body>
    <h1>How to let the user share the website they are on</h1>
    <p>
      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.
    </p>
    <img src="https://placekitten.com/400/300" width=400 height=300>
    <p>
      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.
    </p>
    <hr />
    <button type="button"><span class="icon"></span>Share</button>
  </body>
</html>

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;
    background: #9c9c9c;
    padding: 12px;
    color:  #fff;
    border: 1px solid #9c9c9c;
    border-radius: 8px;
}

button .icon {
  display: inline-block;
  width: 1em;
  height: 1em;
  background-size: 1em;
}

button:hover {
  background: #5089d3ff;
}

@media (prefers-color-scheme: dark) {
  button .icon {
    filter: invert();
  }
}

.share {
  background-image: url('windows.svg');
  color: #fff;
}

.sharemac {
  background-image: url('mac.svg');
  color: #fff;
}
        

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 isMac = navigator.platform.toLowerCase().includes('mac');

// Find out if the user is on a Windows device.
const isWin = navigator.platform.toLowerCase().includes('win');

// For Apple devices or Windows, use the platform-specific share icon.
icon.classList.add(`share${isMac? 'mac' : (isWin? '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.
  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');
});