docs.rodeo

MDN Web Docs mirror

BeforeInstallPromptEvent

{{APIRef}} {{SeeCompatTable}} {{Non-standard_header}} 

The BeforeInstallPromptEvent is the interface of the {{domxref("Window.beforeinstallprompt_event", "beforeinstallprompt")}}  event fired at the {{domxref("Window")}}  object before a user is prompted to “install” a website to a home screen on mobile.

This interface inherits from the {{domxref("Event")}}  interface.

{{InheritanceDiagram}} 

Constructor

Instance properties

Inherits properties from its parent, {{domxref("Event")}} .

Instance methods

Examples

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

<button id="install" hidden>Install</button>

The beforeinstallprompt handler:

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:

installButton.addEventListener("click", async () => {
  if (!installPrompt) {
    return;
  }
  const result = await installPrompt.prompt();
  console.log(`Install prompt was: ${result.outcome}`);
  installPrompt = null;
  installButton.setAttribute("hidden", "");
});

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN