我正在安卓设备上测试Chrome70,它应该可以很好地阻止AddToHomescreen提示符的显示。我不能阻止提示或捕获以供以后使用。每次加载页面时都会继续显示提示符。
文本框已填充,并显示beforeinstallprompt事件正在加载。事件处理程序上的preventDefault不会阻止提示。
为什么?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="theme-color" content="#eaa103">
<link rel="manifest" href="/pwa/manifest.json" />
<title>Some App</title>
<link rel="stylesheet" href="/pwa/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<testbox></testbox>
</div>
<script src="/pwa/js/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
<script>
var deferredPrompt;
window.addEventListener('beforeinstallprompt', function(e) {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
$("testbox").html("beforeinstallprompt loaded");
return false;
});
</script>
</body>
</html>发布于 2019-02-12 01:38:09
根据谷歌开发人员的这篇文章:https://developers.google.com/web/updates/2018/06/a2hs-updates
正在启动Chrome 68...无论是否在
beforeinstallprompt事件中调用了preventDefault(),都会显示迷你信息栏
因此,开发人员目前没有办法在移动端Chrome版本大于67的情况下屏蔽页面上的横幅(它适用于Desktop和低于68的旧移动端版本)。
这里还有更多信息:https://developers.google.com/web/fundamentals/app-install-banners/。如上所述:
迷你信息栏
是安卓系统上Chrome的临时体验
如果被用户取消,它将不会显示,直到经过足够的时间(~3个月)。
我希望这能帮到你。
发布于 2021-05-24 14:20:43
窗口很可能是未定义的,因为您的页面在服务器端呈现。
解决方案:在触发事件之前进行检查,或者通过在运行时运行脚本来确保代码在客户端运行。
如果您使用的是React或Nextjs,请使用useEffect挂钩或componentDidMount。
https://stackoverflow.com/questions/53134483
复制相似问题