根据developers.google.com的说法,可以推迟Web App安装的横幅,但我只能完全取消它。
window.addEventListener('beforeinstallprompt', function(e) {
console.log('beforeinstallprompt Event fired');
e.preventDefault();
return false;
});一种用例是将提示推迟到页面生命周期的后期,例如,在用户执行了某个操作之后,或者在页面底部(表示他们正在使用您的站点)之后。
怎么可能推迟web-app的横幅呢?
发布于 2016-09-04 05:26:38
尝试检查此documentation,如果它可以帮助您的话。
以下是用于延迟提示的完整代码。
var deferredPrompt;
window.addEventListener('beforeinstallprompt', function(e) {
console.log('beforeinstallprompt Event fired');
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
return false;
});
btnSave.addEventListener('click', function() {
if(deferredPrompt !== undefined) {
// The user has had a postive interaction with our app and Chrome
// has tried to prompt previously, so let's show the prompt.
deferredPrompt.prompt();
// Follow what the user has done with the prompt.
deferredPrompt.userChoice.then(function(choiceResult) {
console.log(choiceResult.outcome);
if(choiceResult.outcome == 'dismissed') {
console.log('User cancelled home screen install');
}
else {
console.log('User added to home screen');
}
// We no longer need the prompt. Clear it up.
deferredPrompt = null;
});
}
});有关更多信息,请同时查看此link。
发布于 2019-09-02 22:52:08
我用的是Chrome 76,无法与其他(旧的)答案一起使用。我查看了一下Chrome java脚本调试器,发现需要使用以下代码:
var deferredPrompt;
$(window).on('beforeinstallprompt', function(e) {
console.log('beforeinstallprompt Event fired');
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e.originalEvent;
return false;
});*编辑:只有在使用e.originalEvent注册事件时,才需要通过jquery获取事件。
https://stackoverflow.com/questions/39286385
复制相似问题