JavaScript - 网络浏览器中的通知 API

标签 javascript api google-chrome firefox notifications

我为我的网络服务发出了通知。代码如下所示(前两行是心理捷径):

check_how_many_alerts();
loop count(check_how_many_alerts){
var n = new Notification("Alert!",{ icon: "alert.png", body: variable });
alert_name = variable;
n.onshow = function () {
setTimeout(n.close.bind(n), 10000);}
n.onclick = function() {
    $.ajax({
          url: 'notif_accept.php',
          type: 'GET',
          data: 'id=' + alert_name,
          success: function(output)
                           {
                            alert('success, server says '+output);
                           }, error: function()
                           {
                            alert('something went wrong');
                           }
           });
}
}

问题是当我有两个或多个警报时,它们在 .onclick 函数中都具有相同的 URL。如果 URL 具有不同的“alert_names”,我该怎么办?

最佳答案

如果您想要动态指定 AJAX 调用的 url: 'notif_accept.php',请向 Notification 的原型(prototype)添加一个方法:

Notification.prototype.bind_click = function (url) {
    this.onclick = function() {
        $.ajax({
            url: url,
            type: 'GET',
            data: 'id=' + alert_name,
            success: function(output)
            {
                alert('success, server says '+output);
            }, error: function()
            {
                alert('something went wrong');
            }
        });
    }
}
n = new Notification("Alert!",{ icon: "alert.png", body: variable });
n.bind_click('notif_accept.php');

关于JavaScript - 网络浏览器中的通知 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439227/

相关文章:

django - 序列化器中的循环依赖

api - 带有 CSV 数据的 Google 可视化

ruby - Heroku 和网络抓取

google-chrome - Watir 自动化后如何备份浏览器状态

jquery - 使用 JQuery 绑定(bind)按键事件在 Chrome 中不起作用

javascript - 在 Chrome 控制台中调试 javascript

javascript - 浏览器关闭事件

javascript - 克隆元素并将名称中的索引递增 1

javascript - 选择 javascript 输入 radio 有困难

javascript - 在整个 mocha 测试之前运行异步代码