2014-10-18 77 views
0

我为我的Web服务发出了通知。代码如下所示(两个第一线的精神快捷方式):JavaScript - 浏览器中的通知API

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”?

+0

不知道网址,你正在谈论。显示的代码中没有任何内容涉及URL的 – charlietfl 2014-10-18 11:31:12

回答

1

,如果你想要的是动态指定你url: 'notif_accept.php'的AJAX调用,添加一个方法来Notification的原型:

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'); 
+0

这就是我所需要的。非常感谢你! – bckqs 2014-10-19 04:19:30

+0

不客气! – 2014-10-19 07:19:17