一段js代码模拟实现web消息推送的功能,通过闪烁文档的标题来实现,代码如下:
var titleInit = document.title, isShine = true;
setInterval(function() {
var title = document.title;
if (isShine == true) {
if (/新/.test(title) == false) {
document.title = '【你有新消息】';
} else {
document.title = '【 】';
}
} else {
document.title = titleInit;
}
}, 500);
window.onfocus = function() {
isShine = false;
};
window.onblur = function() {
isShine = true;
};
// for IE
document.onfocusin = function() {
isShine = false;
};
document.onfocusout = function() {
isShine = true;
};
效果如下:
如图示效果
网友评论