I added a share button to my tool pages, then watched it do nothing on half the phones I tested it on. The button was there, it looked fine, and on some browsers tapping it did exactly nothing. That is the worst kind of bug on a tool site: the page looks finished, and the one control a visitor reaches for is dead.
The problem was the Web Share API. navigator.share() is the tidy way to hand a link to the phone's own share sheet — WhatsApp, Telegram, Messages, whatever the visitor already uses. But it is not everywhere. Desktop Chrome has it, mobile Safari has it, but plenty of browsers either lack it entirely or only support it in some contexts. I had written the button as if the API was guaranteed, and it was not.
The fix is boring, and that is the point: check first, then decide what to show.
const shareBtn = document.getElementById('share-btn');
if (navigator.share) {
shareBtn.hidden = false;
shareBtn.addEventListener('click', async () => {
try {
await navigator.share({
title: document.title,
text: 'Save this video with ramrom.io',
url: window.location.href
});
} catch (err) {
// The visitor closed the share sheet. That is not an error.
if (err.name !== 'AbortError') {
console.warn('Share failed:', err);
}
}
});
} else {
shareBtn.hidden = true;
}
Two details in there matter more than they look.
First, the button starts hidden in the HTML. I do not render it and then hide it with JavaScript a moment later — that flash of a button that then vanishes is exactly the sort of thing that makes a page feel broken. The markup has hidden on it from the start, and the script only removes it when navigator.share actually exists. If the API is missing, the visitor never sees a control they cannot use.
Second, the AbortError check. When someone opens the share sheet and then closes it without picking anything, the promise rejects with an AbortError. That is normal behaviour, not a failure, and logging it as an error would fill my console with noise and make real problems harder to spot. Only genuine failures get logged.
There is a second layer of detection worth knowing about. navigator.share existing does not mean the thing you want to share is supported. On some browsers you can share a URL but not files, for example. If you ever share files, navigator.canShare() is the check for that:
const files = [new File(['hello'], 'hello.txt', { type: 'text/plain' })];
if (navigator.canShare && navigator.canShare({ files })) {
// Safe to offer file sharing.
}
I only share a title, some text and a URL, so navigator.share on its own is enough for my pages. But if I ever add file sharing, canShare is the gate I would put in front of it, for the same reason: never show a control that cannot work.
What about the browsers where the button stays hidden? Those visitors lose nothing. They can still copy the link from the address bar, and the copy-link button next to it works everywhere because it uses navigator.clipboard with a plain fallback. The share button is a convenience for the browsers that support it, not a feature the page depends on. That distinction is the whole design: the page must work with nothing but a link box and a download button, and everything else is layered on top only where the browser can actually deliver it.
I test this the unglamorous way: a real Android phone, an old iPhone, and desktop Chrome, tapping the button on each and watching what happens. Emulators and feature tables tell you what should work. A phone in your hand tells you what does.
The lesson generalises well beyond one button. Every browser feature I add to the site gets the same treatment now: detect it, hide the control if it is missing, and make sure the page still does its job without it. Feature detection is a few lines of code, and it is the difference between a page that quietly adapts and a page that shows visitors a button that does nothing.
Top comments (0)