Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Window: beforeinstallprompt event

Limited availability

Thebeforeinstallprompt event fires when the browser has detected that a website can beinstalled as a Progressive Web App.

There's no guaranteed time this event is fired, but it usually happens on page load.

The typical use for this event is when a web app wants to provide its own in-app UI inviting the user to install the app, rather than the generic one provided by the browser. This enables the app to provide more context about the app, explaining to the user why they might want to install it.

In this scenario, the handler for this event will:

  • Keep a reference to theBeforeInstallPromptEvent object that's passed into it
  • Reveal its in-app installation UI (this should be hidden by default, because not all browsers will support installation).

When the user uses the in-app installation UI to install the app, the in-app installation UI calls theprompt() method of the retainedBeforeInstallPromptEvent object to show the installation prompt.

Syntax

Use the event name in methods likeaddEventListener(), or set an event handler property.

js
addEventListener("beforeinstallprompt", (event) => { })onbeforeinstallprompt = (event) => { }

Event type

Event properties

Event methods

BeforeInstallPromptEvent.prompt()Non-standardExperimental

Show a prompt asking the user if they want to install the app. This method returns aPromise that resolves to an object describing the user's choice when they were prompted to install the app.

Examples

In the following example an app provides its own install button, which has anid of"install". Initially the button is hidden.

html
<button hidden>Install</button>

Thebeforeinstallprompt handler:

  • Cancels the event, which prevents the browser displaying its own install UI on some platforms
  • Assigns theBeforeInstallPromptEvent object to a variable, so it can be used later
  • Reveals the app's install button.
js
let installPrompt = null;const installButton = document.querySelector("#install");window.addEventListener("beforeinstallprompt", (event) => {  event.preventDefault();  installPrompt = event;  installButton.removeAttribute("hidden");});

When clicked, the app's install button:

  • Calls theprompt() method of the stored event object, to trigger the installation prompt.
  • Resets its state by clearing theinstallPrompt variable and hiding itself again.
js
installButton.addEventListener("click", async () => {  if (!installPrompt) {    return;  }  const result = await installPrompt.prompt();  console.log(`Install prompt was: ${result.outcome}`);  installPrompt = null;  installButton.setAttribute("hidden", "");});

Specifications

Specification
Manifest Incubations
# onbeforeinstallprompt-attribute

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp