我正在使用浏览器通知,因为它不适用于我想要检查我的JS代码的每个浏览器是否可用.我看了一下mozilla开发者部分:https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API
他们告诉我,我应该使用此代码来检查浏览器是否有通知支持:
if (!("Notification" in window)) {
alert("This browser does not support system notifications");
}
Run Code Online (Sandbox Code Playgroud)
现在我将该代码复制到我的网站中:通知仍然可以在之前工作的浏览器中运行,但它阻止了其他代码的执行(就像它在'check'-code之前所做的那样).
例如:在边缘浏览器中,我收到控制台错误:
'通知未定义'
那么您检查浏览器是否具有通知功能的首选方法是什么?