--- url: "https://web.dev/articles/codelab-make-installable" titulek: "Make it installable | Articles | web.dev" h1: "Make it installable Stay organized with collections Save and categorize content based on your preferences." popis: "In this codelab, learn how to make a site installable using the beforeinstallprompt event." jazyk: "en" kanonicka: "https://web.dev/articles/codelab-make-installable" drobecky: ["Articles", "Make it installable"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:55:13" znaku: 3622 slov: 492 kodovani: "iso-8859-2" sha256: "31cbcebae217c0ffa9ed42dd715340f6e48ec19f9e08d54050ee1de62dd3773a" --- Make it installable Stay organized with collections Save and categorize content based on your preferences. Pete LePage Listen for the beforeinstallprompt event When the browser fires the `beforeinstallprompt` event, that's the indication that the Web App can be installed and an install button can be shown to the user. The `beforeinstallprompt` event is fired when the app meets [the installability criteria](/articles/install-criteria). Capturing the event enables developers to customize the installation and prompt the user to install when they consider it is the best time. Click **Remix to Edit** to make the project editable. Add a `beforeinstallprompt` event handler to the `window` object. Save the `event` as a global variable; we'll need it later to show the prompt. Unhide the install button. Code: `window.addEventListener('beforeinstallprompt', (event) => { // Prevent the mini-infobar from appearing on mobile. event.preventDefault(); console.log('👍', 'beforeinstallprompt', event); // Stash the event so it can be triggered later. window.deferredPrompt = event; // Remove the 'hidden' class from the install button container. divInstall.classList.toggle('hidden', false); });` Handle the install button click To show the install prompt, call `prompt()` on the saved `beforeinstallprompt` event. Calling `prompt()` is done in the install button click handler because `prompt()` must be called from a user gesture. Add a click event handler for the install button. Call `prompt()` on the saved `beforeinstallprompt` event. Log the results of the prompt. Set the saved `beforeinstallprompt` event to null. Hide the install button. Code: `butInstall.addEventListener('click', async () => { console.log('👍', 'butInstall-clicked'); const promptEvent = window.deferredPrompt; if (!promptEvent) { // The deferred prompt isn't available. return; } // Show the install prompt. promptEvent.prompt(); // Log the result const result = await promptEvent.userChoice; console.log('👍', 'userChoice', result); // Reset the deferred prompt variable, since // prompt() can only be called once. window.deferredPrompt = null; // Hide the install button. divInstall.classList.toggle('hidden', true); });` Track the install event Installing a Web App through an install button is only one way users can install it. They can also use Chrome's menu, the mini-infobar, and through [an icon in the omnibox](/articles/promote-install#browser-promotion). You can track all of these ways of installation by listening for the `appinstalled` event. Add an `appinstalled` event handler to the `window` object. Log the install event to analytics or other mechanism. Code: `window.addEventListener('appinstalled', (event) => { console.log('👍', 'appinstalled', event); // Clear the deferredPrompt so it can be garbage collected window.deferredPrompt = null; });` Further reading Congratulations, you app is now installable! Here are some additional things that you can do: [Detect if your app is launched from the home screen](/articles/customize-install#detect-launch-type) [Show the operating system's app install prompt instead](https://developer.chrome.com/blog/app-install-banners-native) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2018-11-05 UTC.