復(fù)制代碼代碼如下: dir: "" onclick: null onclose: null ondisplay: function (event) { onerror: null onshow: null replaceId: "" tag: "" __proto__: Notification addEventListener: function addEventListener() { [native code] } cancel: function cancel() { [native code] } close: function close() { [native code] } constructor: function Notification() { [native code] } dispatchEvent: function dispatchEvent() { [native code] } removeEventListener: function removeEventListener() { [native code] } show: function show() { [native code] } __proto__: Object
dir:設(shè)置消息的排列方向,可取值為 auto (自動(dòng)), ltr (left to right), rtl (right to left)。 tag:為消息添加標(biāo)簽名。如果設(shè)置此屬性,當(dāng)有新消息提醒時(shí),標(biāo)簽相同的消息只顯示在同一個(gè)消息框,后一個(gè)消息框會(huì)替換先前一個(gè),否則出現(xiàn)多個(gè)消息提示框,但是最多值顯示3個(gè)消息框,超過(guò)3個(gè),后繼消息通知會(huì)被阻塞。 onshow:當(dāng)消息框顯示的時(shí)候觸發(fā)該事件; onclick: 當(dāng)點(diǎn)擊消息框的時(shí)候觸發(fā)該事件; onclose:當(dāng)消息關(guān)閉的時(shí)候觸發(fā)該事件; onerror:當(dāng)出現(xiàn)錯(cuò)誤的時(shí)候觸發(fā)該事件; 方法: addEventListener removeEventListener:常規(guī)的添加和移除事件方法; show:顯示消息提醒框; close:關(guān)閉消息提醒框; cancel:關(guān)閉消息提醒框,和 close一樣; 4.createHTMLNotification() 該方法與 createNotification() 不同的是,他以HTML方式創(chuàng)建消息,接受一個(gè)參數(shù): HTML 文件的URL,該方法同樣返回 Notification對(duì)象。 一個(gè)實(shí)例:
復(fù)制代碼代碼如下: !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 html教程