简体   繁体   English

如何在卸载之前关闭页面的所有通知(不使用Window#onunload)?

[英]How can I close all notifications of a page before unload it (without use Window#onunload)?

I have a page that notifies the user about server updates, using window.webkitNotifications . 我有一个页面,使用window.webkitNotifications通知用户有关服务器更新的信息。

It's not a Google Chrome extension. 它不是Google Chrome扩展程序。

I want to close all page notifications before the page unload. 我想在页面卸载前关闭所有页面通知。 I'm trying to do it with: 我正在努力做到:

var notifications = new Array();

// ...
var popup = window.webkitNotifications.createNotification(...);
// ...
notifications.push(popup);

// ...
function closeAll(){
  for (notification in notifications) {
    notifications[notification].cancel();
  }
}

//...
$(window).unload(function() {
  closeAll();
});

But the notifications are not closed when I reloads the page. 但是当我重新加载页面时,通知没有关闭。 I found this issue on Chromium project: https://code.google.com/p/chromium/issues/detail?id=40262 我在Chromium项目中发现了这个问题: https//code.google.com/p/chromium/issues/detail? id = 40262

How can I ensure that page notifications are closed without use the Window#onunload ? 如何在不使用Window#onunload的情况下确保关闭页面通知?

I wonder if you can add some javascript to the notification popup and settimeout withing the actual popup, that way it will close eventually. 我想知道你是否可以通过实际弹出窗口向通知弹出窗口和settimeout添加一些javascript,这样它最终会关闭。

I will have to try and test this out. 我将不得不尝试对此进行测试。

Use this :) 用这个 :)

$(window).on('beforeunload', function() {
    closeAll();
});

使用beforeunload事件而不是unload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM