如何在HTML5通知中包含链接?

Ton*_*bet 5 html5 notifications webkit html5-notifications

我希望能够为每个通知设置链接/永久链接,因此当用户点击它时; 然后他被带到永久链接的位置,

我已经看到这个答案有一个有点不同的解决方案,因为使用了静态链接,

不知怎的,我想:

var noti = window.webkitNotifications.createNotification(
     'http://funcook.com/img/favicon.png', 
     'HTML5 Notification', 
     'HTML5 Notification content...',
     'http://mycustom.dynamic.link.com/'    /* invented code */
)
noti.onclose = function(){ alert(':(') };
noti.onclick = function(){ 
    window.location.href = $(this).url; /* invented code */
};
noti.show();
Run Code Online (Sandbox Code Playgroud)

任何机会?(我真的不喜欢静态html文件解决方案......我想保留这种语法)

Jas*_*wer 9

这样的事怎么样?

var createNotificationWithLink = function(image, title, content, link) {
    var notification = window.webkitNotifications.createNotification(image, title, content);

    notification.onclose = function() {
        alert(':(');
    };

    notification.onclick = function() { 
        window.location.href = link;
    };

    return notification;
};
Run Code Online (Sandbox Code Playgroud)

现在,createNotificationWithLink只要您想创建通知,就可以打电话:

var noti = createNotificationWithLink(
    'http://funcook.com/img/favicon.png',
    'HTML5 Notification',
    'HTML5 Notification content...',
    'http://mycustom.dynamic.link.com/'
);

noti.show();
Run Code Online (Sandbox Code Playgroud)

如果你喜欢(),你也可以noti.show();进入该createNotificationWithLink功能notification.show();.我不知道您是否希望在创建通知时自动显示通知...


Fre*_*red 5

您可以将data可以从onclick事件处理程序中读取的属性作为e.target.data.

var notification = new window.Notification("Hello!",
{
    body: "Hello world!",
    data: "https://www.example.com/?id=" + 123
});

notification.onclick = function(e) {
    window.location.href = e.target.data;
}
Run Code Online (Sandbox Code Playgroud)