Invoking Native Social Sharing with Vanilla JavaScript: A Comprehensive Guide

By: Rajat Kumar | Last Updated: May 07, 2023

Introduction

Social media has become an integral part of our lives, and we all want to share something on our social media platforms at some point. If you are building a web application, you may want to provide your users with a way to share your content on their social media accounts. Fortunately, you can easily invoke native social sharing in JavaScript with just a few lines of code.

Social sharing is a common feature that many web applications need to incorporate. It allows users to easily share content with their friends and followers on different social media platforms. While there are many third-party plugins and libraries that offer social sharing functionality, sometimes it's better to use the native social sharing feature of the device for a more seamless and familiar user experience. In this article, we'll explore how to invoke native social share in JavaScript.

In this article we will learn how we can invoke the native social share intent for any platform and operating system with example.

 

Invoking Native Social Sharing on Mobile Devices

Native social sharing on mobile devices allows users to share content from your web application directly to their social media accounts without leaving the app. To do this, you need to use the navigator.share() method. Following are the simple steps to invoke social share in mobile devices. This steps will also works on tablet and modern desktop devices as well if not you can use other solution that specifically made for desktop devices.

 

Step 1: Check if the browser supports the Web Share API

The first step is to check if the browser supports the Web Share API. This API allows web developers to invoke the native sharing functionality of the device. The Web Share API is supported on most modern browsers, including Chrome, Firefox, Edge, and Safari. To check if the browser supports this API, you can use the following javascript code:

if (navigator.share) {
 alert("Native sharing is supported");
} else {
 alert("Native sharing is not supported");
}

 

Step 2: Create a sharing function

The next step is to create a function that will trigger the native sharing dialog. This function will be called when the user clicks the share button. Here's an example of how to create a function that shares a link:

JavaScript code:


function shareLink() {
 navigator.share({
   title: 'Check out this link',
   text: 'This is a great resource for web developers',
   url: 'https://example.com'
 })
   .then(() => console.log('Shared successfully'))
   .catch(error => console.log('Error sharing:', error));
}


The navigator.share() method takes an object as an argument that contains the data to be shared. In this example, we're sharing a link, so we've included the title, text, and url properties. Once the user selects a sharing option from the native sharing dialog, the then() method will be called, indicating that the sharing was successful. If there's an error, the catch() method will be called.

 

Step 3: Add a share button to your web page

Finally, you need to add a share button to your web page and attach the shareLink() function to its onclick event. Here's an example of how to create a button that triggers the shareLink() function:

<button onclick="shareLink()">Share this link</button>


When the user clicks this button, the shareLink() function will be called, which will open the native sharing dialog.

 

 

Invoking Native Social Sharing on Desktops

Native social sharing on desktops works differently than on mobile devices. On desktops, you need to use the navigator.clipboard.writeText() method to copy the content to the user's clipboard, and then prompt the user to share it on their social media platform of choice.

Here's an example code snippet that shows how to invoke native social sharing on desktops:

const shareButton = document.getElementById('share-button');
const title = 'My awesome post';
const text = 'Check out my awesome post!';
const url = 'https://example.com/my-post';
shareButton.addEventListener('click', async () => {
 try {
   await navigator.clipboard.writeText(`${title}
${text}
${url}`);
   alert('Content copied to clipboard. Please paste it on your social media platform of choice.');
 } catch (error) {
   console.log('Error copying to clipboard', error);
 }
});

The code above listens for a click event on a share button and then uses the navigator.clipboard.writeText() method to copy the content to the user's clipboard. It then prompts the user to paste the content on their social media platform of choice.

Note that the navigator.clipboard.writeText() method may not work on all browsers, so you may need to use a polyfill or provide an alternative sharing mechanism for unsupported browsers.

 

Example Snapshot for Mobile Native Share Intent Box

 

 

 

Example Snapshot for Desktop Native Share Intent Box

 

 

Conclusion

Incorporating native social sharing functionality in your web application can be a great way to provide a seamless and familiar user experience. With the Web Share API, it's easy to invoke the native sharing dialog in JavaScript. By following the steps outlined in this article, you should be able to implement native social sharing in your web application quickly and easily.

Views