我为我的网络服务发出了通知。代码如下所示(前两行是心理捷径):
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/