bckqs
bckqs

Reputation: 3

JavaScript - Notification API in webbrowser

I made a notifications for my web service. The code looks like this (two first lines are mental shortcut):

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');
                           }
           });
}
}

The problem is when I have two or more alerts, all of them have the same URL in .onclick function. What should I do to have URLs with different "alert_names"?

Upvotes: 0

Views: 120

Answers (1)

Nicolas Straub
Nicolas Straub

Reputation: 3411

if what you want is to dynamically specify you're url: 'notif_accept.php' for the AJAX call, add a method to Notification's 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');

Upvotes: 1

Related Questions