我正在处理浏览器通知,因为并非所有浏览器都支持它,所以我想在我的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");
}
我将那段代码复制到了我的网站;通知在之前能够运行的浏览器中仍然能够工作,但会阻塞其他代码的执行(就像“check”代码之前一样)。
例如:在Edge浏览器中,我收到控制台错误:
'Notification is undefined'
那么,您认为最佳方法是什么来检查浏览器是否具备通知能力?
("Notification" in window)
是false
,那么你的代码必须避免使用任何 API。 - Pointy