一段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;
};
效果如下图:
本文介绍了一段使用JavaScript编写的代码,该代码能够实现在网页上通过文档标题闪烁的方式来提醒用户有新的消息。此方法适用于不需要复杂通知机制的简单场景。
1148

被折叠的 条评论
为什么被折叠?



