The Web Share API is a valuable feature that can enhance your website's functionality and user experience. It allows you to share text, URLs, or files with other apps or services using the native sharing mechanism of the device.
In simple language,
The Web Share API is a feature that enables web apps to share content with other apps on the device, using the native share options provided by the system. It has two methods,navigator.share()and navigator.canShare(), which allow web developers to check and invoke the sharing mechanism.
Well, It works only on mobile devices, and is limited to browsers, and requires user activation and secure context.
But what do we mean by native sharing well, I have attached an image below to tell you more about native share read more more about here
The Web Share API allows web apps to share links, text, files, audio, and video with other apps on the device, using the native share options provided by the system. This way, web apps can have the same sharing capabilities as platform-specific apps.
But good things always have limitations.
// Define the data to share
const shareData = {
title: "Rajneesh is tired",
text: "But see he is trying",
url: "https://www.linkedin.com/in/iamrajneesh/",
};
// Get the share button element
const shareBtn = document.getElementById("share-btn");
// Add a click event listener to the button
shareBtn.addEventListener("click", async () => {
// Check if the Web Share API is supported
if (navigator.share) {
// Try to share the data
try {
await navigator.share(shareData);
console.log("Data shared successfully");
} catch (err) {
// Handle any errors
console.error("Error while sharing:", err);
}
} else {
// Fallback to some other sharing method
console.log("Web Share API not supported");
}
});
if (navigator.canShare && navigator.canShare({ files: filesArray }))
navigator.share({
files: filesArray, //or using arrays refer to the end comment
title: 'Jauns Photos',
text: 'ek haadasa hee to hai aur vo ye hai aaj tak baat nahin kahee gaee baat nahin kahee gaee',
})
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
} else {
console.log(`Your system doesn't support sharing files.`);
}
//const input = document.getElementById("file-input");
//const file = input.files[0];
This is a very small and powerful feature and I was eager to share it with you all also it's too easy to use. and so i did not want to put too much effort because the self-details by W3c and Mozilla docs are really very awesome. So, if you have understood my article and want to take it to the next level, I will recommend visiting the following links. Or maybe, when you use WebShare API in the production future, make sure to check.
If you would like to manifest more in detail you visit the following links:
https://developer.mozilla.org/en-US/docs/Web/Manifest/share_target
https://github.com/w3c/web-share
By joining my newsletter, you'll receive updates on the latest technology news and have the opportunity to learn more about coding.