2014-10-18 39 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