检查浏览器通知是否可用 [英] Check if browser notification is available

查看:99
本文介绍了检查浏览器通知是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用浏览器通知,因为它不适用于我要检入 JS代码的每个浏览器(如果可用)。

I am working with browser notifications and because it is not available for every browser I want to check in my JS-code, if it is available.

我在Mozilla开发人员部分中进行了查看:

https://developer.mozilla.org/zh-CN/docs/Web/API/Notifications_API/Using_the_Notifications_API

I took a look in the Mozilla developer section:
https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API

他们告诉我,我应该使用此代码检查浏览器是否具有通知支持:

They tell me that I should use this code to check whether a browser has notification support or not:

  if (!("Notification" in window)) {
    alert("This browser does not support system notifications");
  }

我将该代码复制到了我的网站中;通知仍然可以在以前使用过的浏览器中运行,但是会阻止其他代码的执行(就像在检查代码之前一样)。

I copied that code into my website; the notification still works in the browsers where it worked before but it blocks the execution of other code (as it did before the 'check'-code).

例如:在边缘浏览器中,我收到控制台错误:

For example: in the edge browser, I get the console error:


 'Notification is undefined'


那么检查您的浏览器是否具有通知功能的首选方法是什么?

So what is the preferred method from you to check whether a browser has notification capability?

推荐答案

以下代码将检查浏览器是否与Web通知API兼容

The following code would check whether the browser is compatible with the web notification API

if ('Notification' in window) {
  // API supported
} else {
  // API not supported
}

这篇关于检查浏览器通知是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆