// Store the deferredPrompt for later use let deferredPrompt; let installButton; // When the page loads window.addEventListener('load', () => { // Find your install button (create it if needed) installButton = document.getElementById('install-button'); // Initially hide the button if (installButton) { installButton.style.display = 'none'; } }); // Listen for the beforeinstallprompt event window.addEventListener('beforeinstallprompt', (e) => { // Prevent Chrome 67 and earlier from automatically showing the prompt e.preventDefault(); // Stash the event so it can be triggered later deferredPrompt = e; // Show the install button if (installButton) { installButton.style.display = 'block'; // Add click event to the button installButton.addEventListener('click', async () => { // Hide the button once clicked installButton.style.display = 'none'; // Show the install prompt deferredPrompt.prompt(); // Wait for the user to respond to the prompt const { outcome } = await deferredPrompt.userChoice; console.log(`User response to the install prompt: ${outcome}`); // Clear the deferredPrompt variable deferredPrompt = null; }); } }); // Listen for the appinstalled event window.addEventListener('appinstalled', (evt) => { // Log install to analytics console.log('PWA was installed'); });

Harry Winston

This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies. Read more about cookies