浏览器状态授权(允许通知消息)
if (window.Notification.permission != 'granted') {
Notification.requestPermission(function (status) {
//status是授权状态,如果用户允许显示桌面通知,则status为'granted'
console.log('status: ' + status);
//permission只读属性:
// default 用户没有接收或拒绝授权 不能显示通知
// granted 用户接受授权 允许显示通知
// denied 用户拒绝授权 不允许显示通知
Notification.permission = status;
// console.log('permission:lfc ' + permission);
});
}
判断浏览器是不是在当前页
var OnOFF = 1;//0 不是当前页 1,当前页
// 各种浏览器兼容
var hidden, state, visibilityChange;
if (typeof document.hidden !== "undefined") {
hidden = "hidden";
visibilityChange = "visibilitychange";
state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
state = "webkitVisibilityState";
}
// 添加监听器,在title里显示状态变化
document.addEventListener(visibilityChange, function() {
console.log("document[state]",document[state])
if (document[state] == "visible") {
OnOFF = 1;
}else{
OnOFF = 0;
}
}, false);