Skip to content

Commit

Permalink
Improve navigator.share example (#227)
Browse files Browse the repository at this point in the history
Better error handling, was failing silently when not supported.

New version is similar to code example at https://developer.mozilla.org/en-US/docs/Web/API/Navigator/canShare#examples

Co-authored-by: Brian Thomas Smith <[email protected]>
  • Loading branch information
mrienstra and bsmth authored Jul 17, 2024
1 parent d955ab6 commit 878a5da
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions web-share/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@ <h1>Sharing MDN</h1>
title: 'MDN',
text: 'Learn web development on MDN!',
url: 'https://developer.mozilla.org',
}
};

const btn = document.querySelector('button');
const resultPara = document.querySelector('.result');

btn.addEventListener('click', () => {
if (!navigator.canShare) {
resultPara.textContent = 'Web Share API not available';
return;
}
if (!navigator.canShare(shareData)) {
resultPara.textContent = 'Share data unsupported, disallowed, or invalid';
return;
}
navigator.share(shareData)
.then(() =>
resultPara.textContent = 'MDN shared successfully'
resultPara.textContent = 'MDN shared successfully';
)
.catch((e) =>
resultPara.textContent = 'Error: ' + e
resultPara.textContent = 'Error: ' + e;
)
});
</script>
Expand Down

0 comments on commit 878a5da

Please sign in to comment.