非标准
该特性是非标准的,请尽量不要在生产环境中使用它!
在一个用户被提示”安装“一个网站到移动设备的一个主屏幕之前, BeforeInstallPromptEvent
被Window.onbeforeinstallprompt
处理程序触发。
该接口继承自Event
接口。
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 8.571428571428571%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-20 0 700 60" preserveAspectRatio="xMinYMin meet"><a xlink:href="https://developer.mozilla.org/zh-CN/docs/Web/API/BeforeInstallPromptEvent" target="_top"><rect x="1" y="1" width="240" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="121" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">BeforeInstallPromptEvent</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
Constructor
new window.BeforeInstallPromptEvent(name, eventInitOptions)
- Creates a new
BeforeInstallPromptEvent
.
Properties
Inherits properties from its parent, Event
.
BeforeInstallPromptEvent.platform
只读- Returns a
DOMString
containing the platform on which the event was dispatched. BeforeInstallPromptEvent.userChoice
只读- Returns a
Promise
that resolves to aDOMString
containing either 'installed' or 'dismissed'.
Methods
BeforeInstallPromptEvent.prompt()
- Allows a developer to show the install prompt at a time of their own choosing. This method returns a
Promise
.
Example
window.addEventListener("beforeinstallprompt", function(e) { // log the platforms provided as options in an install prompt console.log(e.platforms); // e.g., ["web", "android", "windows"] e.userChoice.then(function(outcome) { console.log(outcome); // either "installed", "dismissed", etc. }, handleError); });
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support |
44.0 [1] |
未实现 | 未实现 | 未实现 | 未实现 |
prompt() method. |
45.0 [1] | 未实现 | 未实现 | 未实现 | 未实现 |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | 未实现 |
44.0 [1] |
未实现 | 未实现 | 未实现 | 未实现 |
44.0 [1] |
prompt() method. |
未实现 | 45.0 [1] | 未实现 | 未实现 | 未实现 | 未实现 | 45.0 [1] |
[1] Behind the flagchrome://flags/#bypass-app-banner-engagement-checks