Get started with sharer.js CDN

MIT licensed

Sharer.js is a lightweight library for adding social sharing buttons to websites.

Tags:
  • social
  • share
  • facebook
  • twitter
  • google
  • plus
  • linkedin
  • email
  • telegram
  • whatsapp
  • viber
  • hackernews
  • vk
  • reddit

Stable version

Copied!

How to start using sharer.js CDN


// Include the sharer.min.js library from the CDN
const script = document.createElement('script');
script.src = 'https://cdn.cdnhub.io/sharer.js/0.5.1/sharer.min.js';
document.head.appendChild(script);

// Wait for the library to be loaded before using it
script.addEventListener('load', () => {
  // Initialize the sharer object with your API key
  const myShare = new YASHOC('your_api_key');

  // Configure the buttons
  myShare.addService('VKontakte', 'vk');
  myShare.addService('Facebook', 'facebook');
  myShare.addService('Twitter', 'twitter');

  // Create a button with the ID 'share-button'
  const shareButton = document.createElement('button');
  shareButton.id = 'share-button';
  shareButton.innerText = 'Share';
  document.body.appendChild(shareButton);

  // Attach the share function to the button
  shareButton.addEventListener('click', () => {
    myShare.share(
      document.body.textContent, // The content to be shared
      'https://example.com', // The URL of the current page
      'Share this example', // The title of the content
      'Share' // The name of the button
    );
  });
});
Copied!
Copied!

All versions