javascript - HTML 5 通知在 Chrome 中无法在本地运行?

标签 javascript html google-chrome

我找到了以下 HTML 通知示例,它在 Chrome 和 Firefox 中运行良好。下载并在本地试用后,它不再适用于 Chrome。这是预期的行为(Chrome 出于某种原因在本地阻止通知)还是有任何其他原因导致它不起作用?

<!DOCTYPE html>
<html>

    <body>

        <button onclick="notifyMe()">Notify me!</button>

        <script>
        function notifyMe() {
          // Let's check if the browser supports notifications
          if (!("Notification" in window)) {
            alert("This browser does not support desktop notification");
          }

          // Let's check if the user is okay to get some notification
          else if (Notification.permission === "granted") {
            // If it's okay let's create a notification
            var notification = new Notification("Hi there!");
          }

          // Otherwise, we need to ask the user for permission
          // Note, Chrome does not implement the permission static property
          // So we have to check for NOT 'denied' instead of 'default'
          else if (Notification.permission !== 'denied') {
            Notification.requestPermission(function (permission) {

              // Whatever the user answers, we make sure we store the information
              if(!('permission' in Notification)) {
                Notification.permission = permission;
              }

              // If the user is okay, let's create a notification
              if (permission === "granted") {
                var notification = new Notification("Hi there!");
              }
            });
          }

        }
        </script>

    </body>
</html>

最佳答案

根据W3C Specification只要文件托管在某处,您所拥有的看起来就没问题。这是一个不错的 notification repo , 与 live demo .

screenshot

关于javascript - HTML 5 通知在 Chrome 中无法在本地运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24492107/

相关文章:

jquery - 在文档单击时取消固定 div

html - @font-face url 指向本地文件

Javascript 原型(prototype)继承?

javascript - 网络 worker : How to prevent that file gets loaded from cache?

javascript - 是否需要以相反的顺序声明 javascript 函数?

javascript - 重定向的提交按钮不起作用

javascript - Chrome 与 d3 的foreignObject bug?

javascript - Electron:无法滚动到 div 的底部

javascript - 如何从 Instagram API 获取裁剪后的图像?

windows - 如何在没有ActiveX的情况下直接在网页上安装谷歌浏览器