Skip to content

Instantly share code, notes, and snippets.

@4r7ur-sid
Created October 21, 2022 06:03
Show Gist options
  • Save 4r7ur-sid/e28291bf5ad8583e79d2df061b7318ef to your computer and use it in GitHub Desktop.
Save 4r7ur-sid/e28291bf5ad8583e79d2df061b7318ef to your computer and use it in GitHub Desktop.
JavaScript | Function For Native Sharing on Mobile and Desktop
const _share = (url,title,text)=>{
// Check if native share is supported
if(navigator.share){
navigator.share({
url,
title,
text
})
.then(()=>{
// Analytics event for share
})
.catch((error)=>{
// Sentry error
});
}
else{
// Fallback to a 3rd party share
}
}
@4r7ur-sid
Copy link
Author

4r7ur-sid commented Oct 21, 2022

This allows users to share your blog/page via their systems default sharing options.

It works excellent on mobile devices as users can share via WhatsApp, copy link, email, Social Media etc. via their native apps.

This is not a replacement to tools like Addthis and not all browsers supports this but it gives a familiar sharing experience to user.

One should always implement a fallback mechanism in case the browser doesn't support this.

I am personally using this on my Personal website and have received positive response from users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment