Skip to content

Commit

Permalink
Improve navigator.share example
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
  • Loading branch information
mrienstra authored Sep 17, 2023
1 parent df1f3a0 commit 56af719
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 56af719

Please sign in to comment.