Created
October 2, 2024 06:01
-
-
Save mirojones/e0c1b51c179b7fd236c0b46b1452ae68 to your computer and use it in GitHub Desktop.
Simple JavaScript share button using navigation.share
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button id="shareBtn">Share</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById('shareBtn').addEventListener("click", async () => { | |
var shareData = { | |
title: document.title, | |
url: window.location.href | |
} | |
try { | |
await navigator.share(shareData); | |
console.log("Data was shared successfully"); | |
} catch (err) { | |
console.error("error:", err.message); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment