Skip to content

Instantly share code, notes, and snippets.

@saravanakumarputta
Created June 21, 2020 11:12
Show Gist options
  • Save saravanakumarputta/91d472049bc0d95fc8bb4c7f92d470ac to your computer and use it in GitHub Desktop.
Save saravanakumarputta/91d472049bc0d95fc8bb4c7f92d470ac to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Share API</title>
<style>
</style>
</head>
<body>
<button id="share">Share!!!</button>
<script>
window.onload = function () {
document.getElementById('share').addEventListener('click', (e) => {
//Check if the Web Share API is being supported by the browser
if (navigator.share) {
navigator.share({
title: 'JS snippets, tips and data structures and whatnot libraries too!! @ one place',
text: 'Javascript tips, snippets, and Data Structure will be posted. Anyone interested in sharing their knowledge in this publication, you are welcome!',
url: 'https://medium.com/awesome-javascript'
})
.then(() => console.log('Share was successful.'))
.catch((error) => {
// You application logic if the share is cancelled by the user or fails
console.log('Sharing failed', error)
});
} else {
// Browser doens't have the support to use the native share functionality
// Can use this block as fallback method to show your preferable sharing options
}
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment