--- title: Window.onbeforeinstallprompt slug: Web/API/Window/onbeforeinstallprompt tags: - Window.onbeforeinstallprompt - beforeinstallprompt translation_of: Web/API/Window/onbeforeinstallprompt ---
{{ ApiRef() }}
Window.onbeforeinstallprompt
属性是一个事件处理程序, 用于处理一个{{event("beforeinstallprompt")}}, 当一个Web清单存在时,它将在移动设备上发送,但是在提示用户将网站保存到主屏幕之前。
window.addEventListener("beforeinstallprompt", function(event) { ... }); window.onbeforeinstallprompt = function(event) { ...};
The following example uses the beforeinstallprompt function to verify that it is an appropriate time to display an installation prompt to the user. If it is not, the event is redispatched.
let isTooSoon = true; window.addEventListener("beforeinstallprompt", function(e) { if (isTooSoon) { e.preventDefault(); // Prevents prompt display // Prompt later instead: setTimeout(function() { isTooSoon = false; e.prompt(); // Shows prompt }, 10000); } // The event was re-dispatched in response to our request // ... });
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatChrome(45.0)}} [1] | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | {{ CompatNo() }} | {{CompatChrome(45.0)}} [1] | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} | {{ CompatNo() }} | {{CompatChrome(45.0)}} [1] |
[1] Behind the flagchrome://flags/#bypass-app-banner-engagement-checks
Specification | Status | Comment |
---|---|---|
{{SpecName('Manifest', '#onbeforeinstallprompt-attribute', 'Window.onbeforeinstallprompt')}} | {{Spec2('Manifest')}} | Initial specification. |