html5 桌面提醒:Notifycations应用介绍

2019-01-28 20:07:08于丽

  close:关闭消息提醒框;
  cancel:关闭消息提醒框,和 close一样;
4.createHTMLNotification()
  该方法与 createNotification() 不同的是,他以HTML方式创建消息,接受一个参数: HTML 文件的URL,该方法同样返回 Notification对象。
一个实例:

复制代码

<!DOCTYPE HTML>
<html>
<head>
<title>notifications in HTML5</title>
</head>
<body>
<form>
<input id="trynotification" type="button" value="Send notification" />
</form>
<script type="text/javascript">
document.getElementById("trynotification").onclick = function(){
notify(Math.random());
};
function notify(tab) {
if (!window.webkitNotifications) {
return false;
}
var permission = window.webkitNotifications.checkPermission();
if(permission!=0){
window.webkitNotifications.requestPermission();
var requestTime = new Date();
var waitTime = 5000;
var checkPerMiniSec = 100;
setTimeout(function(){
permission = window.webkitNotifications.checkPermission();
if(permission==0){
createNotification(tab);
}else if(new Date()-requestTime<waitTime){
setTimeout(arguments.callee,checkPerMiniSec);
}
},checkPerMiniSec);
}else if(permission==0){
createNotification(tab);
}
}
function createNotification(tab){
var showSec = 10000;
var icon = "http://tech.baidu.com/resource/img/logo_news_137_46.png";
var title = "[" + new Date().toLocaleTimeString() + "] close after " + (showSec/1000) + " seconds";
var body = "hello world, i am webkitNotifications informations";
var popup = window.webkitNotifications.createNotification(icon, title, body);
popup.tag = tab;
popup.ondisplay = function(event) {
setTimeout(function() {
event.currentTarget.cancel();
}, showSec);
}
popup.show();
}
</script>
</body>
</html>