Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript删除加载视图中的所有Chrome通知?_Javascript_Google Chrome_Notifications - Fatal编程技术网

如何使用JavaScript删除加载视图中的所有Chrome通知?

如何使用JavaScript删除加载视图中的所有Chrome通知?,javascript,google-chrome,notifications,Javascript,Google Chrome,Notifications,这是我在Google Chrome中显示通知的代码 如何在代码中关闭通知 document.addEventListener('DOMContentLoaded',function(){ 如果(!通知){ 警报('您的浏览器中没有桌面通知。请尝试使用Chromium'); 返回; } if(Notification.permission!=“已授予”) Notification.requestPermission(); }); 函数notifyMe(){ if(Notification.perm

这是我在Google Chrome中显示通知的代码

如何在代码中关闭通知

document.addEventListener('DOMContentLoaded',function(){
如果(!通知){
警报('您的浏览器中没有桌面通知。请尝试使用Chromium');
返回;
}
if(Notification.permission!=“已授予”)
Notification.requestPermission();
});
函数notifyMe(){
if(Notification.permission!=“已授予”)
Notification.requestPermission();
否则{
var通知=新通知('测试'{
图标:'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
正文:“测试”,
});
notification.onclick=function(){
窗口打开(“http://stackoverflow.com/a/13328397/1269037");
};
}
}

这很简单,每个通知对象都有
close()
方法。在关闭窗口之前,只需将它们推到一个数组中,并对每个对象调用
close()

var notify=[];

for(var i=0; i<=4;i++){
  var notification = new Notification('test', {
  icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
  body: "test"+i
  });                                  //create some notifications
  notify.push(notification);
}

function removeAllNotifys()
{
  for(var i=0; i<notify.length;i++){
    notify[i].close();                 //remove them all  
  }
}

window.onbeforeunload = removeAllNotifys; 
var notify=[];

对于(var i=0;我不确定您在这里的意思。用户是否将关闭通知-如果保存句柄
var w=window,则可以关闭窗口。打开(“http://stackoverflow.com/a/13328397/1269037“,“_blank”);
使用
w.close();
除非加载其他原点会阻止您再次访问该窗口