Webkit 桌面通知 学习笔记 (HTML5 webkitNotifications)
1 function notify() { 2 if (window.webkitNotifications) { //判断是否支持该功能 3 if (window.webkitNotifications.checkPermission() == 0) { //判断是否允许弹出桌面通知 4 //文本模式创建通知 5 var deskBox = window.webkitNotifications.createNotification('image.png', '标题', '内容'); 6 //当显示时调用 7 deskBox.ondisplay = function(event) { 8 //自动关闭 9 setTimeout(function() {10 event.currentTarget.cancel();11 }, 10 * 1000);12 };13 //当出错时调用14 deskBox.onerror = function() {};15 //当关闭时调用16 deskBox.onclose = function() {};17 //当点击时调用18 deskBox.onclick = function(event) { 19 //点击跳转页面20 window.focus();21 //关闭通知22 event.currentTarget.cancel();23 };24 //25 deskBox.replaceId = 'box1';26 //显示通知27 deskBox.show(); 28 //关闭通知29 //deskBox.cancel(); 30 31 //URl模式创建通知 32 var deskBox2 = window.webkitNotifications.createHTMLNotification("http://www.baidu.com/");33 deskBox2.replaceId = "box2";34 //延时显示通知35 setTimeout(function(){36 deskBox2.show();37 },5000);38 39 } else {40 //询问用户是否允许提示41 window.webkitNotifications.requestPermission(notify);42 }43 44 45 }46 47 }
TAG: