<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" /> <title>提示</title> <style> </style> </head> <body> <button onclick="alterNotification()">彈出提示</button> <label id="note"></label> </body> <script> window.onload = function () { if(!window.Notification){ alert("瀏覽器不支持Notification"); } }; function alterNotification() { // 只會是granted, denied, 或default. // 其中granted表示用戶容許通知,denied表示用戶嫌棄你,default表示用戶目前尚未管你。 if (Notification.permission != "granted") { Notification.requestPermission(function (permission) { if (permission == "denied") { alert("用戶未贊成彈窗。"); return; } }); } var notice = new Notification("通知", { body: "Content", icon: "image/webwxgeticon.jpg" }); notice.onclick = function () { document.getElementById("note").innerHTML = "已成功通知"; }; } </script> </html>
效果以下:html
瀏覽器測試:FireFox 沒有問題,Chrome 支持網絡,但對本地文件,沒法彈出提示。IE9不支持window.Notification。html5
本文僅記錄本身寫的一個小Demo,更多關於Notification的內容,請點擊下文連接。web
http://www.zhangxinxu.com/wordpress/2016/07/know-html5-web-notification/瀏覽器