JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<a href="https://example.com" class="btn-share" target="_blank">
Share on Facebook
</a>
JavaScript
$(".btn-share").click(function(clickEvent){
var self = $(this);
// Web Share API
if (navigator.share){
clickEvent.preventDefault();
navigator.share({
title: 'This is example website',
url: 'https://example.com'
})
.then(function() {
console.log('Successful share');
})
.catch(function(error) {
console.log('Error sharing:', error);
window.open($(self).attr('href'), '_blank');
});
}
});