ব্যবহারকারীরা যে ওয়েবসাইটটিতে আছেন তা কীভাবে শেয়ার করবেন

ব্যবহারকারীকে তাদের ওয়েবসাইট শেয়ার করতে দেওয়া একটি সাধারণ ওয়েব অ্যাপ প্যাটার্ন যা আপনি অনেক নিউজ সাইট, ব্লগ বা শপিং সাইটে খুঁজে পেতে পারেন। যেহেতু লিঙ্ক করা ওয়েবের সুপার পাওয়ারগুলির মধ্যে একটি, তাই আশা হল সেই সমস্ত ব্যবহারকারীদের কাছ থেকে ট্র্যাফিক অর্জন করা যারা সোশ্যাল নেটওয়ার্কিং সাইটে শেয়ার করা লিঙ্কটি দেখেন, বা যারা এটি চ্যাট বার্তার মাধ্যমে বা এমনকি ইমেলের মাধ্যমে সাধারণ পুরানো স্কুলে পান।

আধুনিক উপায়

ওয়েব শেয়ার API ব্যবহার করে

ওয়েব শেয়ার API ব্যবহারকারীকে একটি শিরোনাম এবং বর্ণনামূলক পাঠ্য সহ তারা যে পৃষ্ঠায় রয়েছে তার URL এর মতো ডেটা ভাগ করতে দেয়৷ ওয়েব শেয়ার API-এর navigator.share() পদ্ধতিটি ডিভাইসের নেটিভ শেয়ারিং মেকানিজমকে আহ্বান করে। এটি একটি প্রতিশ্রুতি প্রদান করে এবং ভাগ করা ডেটার সাথে একটি একক যুক্তি নেয়৷ সম্ভাব্য মান হল:

  • url : একটি স্ট্রিং শেয়ার করা URL প্রতিনিধিত্ব করে।
  • text : শেয়ার করা টেক্সট প্রতিনিধিত্বকারী একটি স্ট্রিং।
  • title : শেয়ার করা একটি শিরোনাম প্রতিনিধিত্বকারী একটি স্ট্রিং। ব্রাউজার দ্বারা উপেক্ষা করা হতে পারে.

ব্রাউজার সমর্থন

  • ৮৯
  • 93
  • 12.1

উৎস

ক্লাসিক উপায়

একটি সামাজিক নেটওয়ার্কিং সাইটের শেয়ার অভিপ্রায় ব্যবহার করে

সমস্ত ব্রাউজার এখনও ওয়েব শেয়ার API সমর্থন করে না। ফলব্যাক হল এইভাবে আপনার টার্গেট শ্রোতাদের সবচেয়ে জনপ্রিয় সোশ্যাল নেটওয়ার্কিং সাইটগুলির সাথে একীভূত করা৷ একটি জনপ্রিয় উদাহরণ হল টুইটার, যার ওয়েব ইন্টেন্ট URL একটি পাঠ্য এবং একটি URL ভাগ করার অনুমতি দেয়৷ পদ্ধতিতে সাধারণত একটি URL তৈরি করা এবং এটি একটি ব্রাউজারে খোলা থাকে।

UI বিবেচনা

অপারেটিং সিস্টেম বিক্রেতাদের UI নির্দেশিকা অনুযায়ী প্ল্যাটফর্মের প্রতিষ্ঠিত শেয়ার আইকনকে সম্মান করা একটি চমৎকার স্পর্শ।

  • উইন্ডোজ:
  • আপেল:
  • অ্যান্ড্রয়েড এবং অন্যান্য অপারেটিং সিস্টেম:

প্রগতিশীল বর্ধিতকরণ

নীচের স্নিপেটটি ওয়েব শেয়ার API ব্যবহার করে যখন এটি সমর্থিত হয়, তারপর টুইটারের ওয়েব ইন্টেন্ট 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');
});

আরও পড়া

ডেমো

এইচটিএমএল

<!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>

সিএসএস


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

জেএস


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