Chromium browsers (Chrome, Edge, Samsung Internet, Opera) fire a beforeinstallprompt event when your site meets the install criteria. You can catch it, keep it, and show the install dialog from your own button at a better moment.
The code
let deferredPrompt = null;
const btn = document.querySelector('#install');
window.addEventListener('beforeinstallprompt', (event) => {
event.preventDefault(); // stop the mini-infobar on Android
deferredPrompt = event; // keep it for later
btn.hidden = false; // now it makes sense to show the button
});
btn.addEventListener('click', async () => {
if (!deferredPrompt) return;
deferredPrompt.prompt(); // must be called from a user gesture
const { outcome } = await deferredPrompt.userChoice; // 'accepted' or 'dismissed'
analytics('install_prompt', { outcome });
deferredPrompt = null; // each event can be used only once
btn.hidden = true;
});
window.addEventListener('appinstalled', () => {
btn.hidden = true;
analytics('app_installed');
});
prompt() can only be called once per event. If the user dismisses it, Chrome fires the event again later (after a cool-down), so keep listening.
When to show it
The best install prompts appear after the user has seen value:
- after they finish a task (a booking, a saved note, a completed lesson);
- on their second or third visit;
- next to a feature that works better installed (offline reading, notifications).
Avoid showing a full-screen install banner on the first page view. It annoys people, and it has the opposite effect.
iPhone and iPad
beforeinstallprompt does not exist in Safari. Detect iOS and, if the app is not already running standalone, show short instructions instead:
const isIOS = /iphone|ipad|ipod/i.test(navigator.userAgent) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
const standalone = matchMedia('(display-mode: standalone)').matches || navigator.standalone;
if (isIOS && !standalone) showHint('Tap the Share button, then "Add to Home Screen".');
See install on iPhone and iPad for wording and screenshots.
Hide the button when installed
@media (display-mode: standalone) { #install { display: none; } }
Ready-made components
- pwa-install by Gleb Khmyznikov: a web component with polished install dialogs for Chromium, with gallery screenshots, and step-by-step guides for iOS, iPadOS and macOS Safari.
- Our install button generator writes the code above, with your own text and an iOS hint.
PWA.live uses this exact pattern: the install icon in the header appears only when your browser can install the site.
Compatibilidad de navegadores
| Función | Chrome / Edge desktop | Chrome Android | Safari iOS / iPadOS | Safari macOS | Firefox desktop | Firefox Android | Samsung Internet |
|---|---|---|---|---|---|---|---|
| beforeinstallprompt (own install button) | Chrome / Edge desktop: Sí | Chrome Android: Sí | Safari iOS / iPadOS: No | Safari macOS: No | Firefox desktop: No | Firefox Android: No | Samsung Internet: Sí |
| Install from the browser | Chrome / Edge desktop: Sí (Address bar icon and menu) | Chrome Android: Sí (Installs a WebAPK) | Safari iOS / iPadOS: Sí (Share > Add to Home Screen (manual)) | Safari macOS: Sí (File > Add to Dock (Safari 17+)) | Firefox desktop: Parcial (Windows only, Firefox 143+) | Firefox Android: Sí (Add to Home screen) | Samsung Internet: Sí |