js实现桌面通知
2016-01-20 15:44
627 查看
<!DOCTYPE html> <html> <head> <title>Google 桌面通知</title> <meta name="generator" content="editplus" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <meta http-equiv='content-type' content='text/html; charset=utf-8' /> </head> <body> <button id='btn'>显示桌面通知</button> <script type='text/javascript'> document.querySelector("#btn").addEventListener('click', notify, false); function notify() { showMsgNotification('这里填入标题','这里是消息的内容'); } function showMsgNotification(title, msg, icon) { var options = { body: msg, icon: icon||"image_url" }; var Notification = window.Notification || window.mozNotification || window.webkitNotification; if (Notification && Notification.permission === "granted") { var instance = new Notification(title, options); instance.onclick = function() { // Something to do }; instance.onerror = function() { // Something to do }; instance.onshow = function() { // Something to do // setTimeout(instance.close, 3000); setTimeout(function () { instance.close(); },3000) console.log(instance.body) }; instance.onclose = function() { // Something to do }; console.log(instance) } else if (Notification && Notification.permission !== "denied") { Notification.requestPermission(function(status) { if (Notification.permission !== status) { Notification.permission = status; } // If the user said okay if (status === "granted") { var instance = new Notification(title, options); instance.onclick = function() { // Something to do }; instance.onerror = function() { // Something to do }; instance.onshow = function() { // Something to do setTimeout(instance.close, 3000); }; instance.onclose = function() { // Something to do }; } else { return false } }); } else { return false; } } </script> </body> </html>
必须在服务器环境下
相关文章推荐
- js验证是否是数字,支持正负数小数
- Javascript诞生记
- 毫秒转换成js年月日时分秒,标准时间转换js
- ES 5、ES 6变量和函数声明以及作用域总结
- Javascriptl历史-继承机制的设计思想
- extjs和js定时器
- BSON与JSON的区别
- 原始js调用 选中事件
- js模拟键盘或遥控器操作(加强版)
- JS获取鼠标坐标位置实例分析
- DOM性能瓶颈与Javascript性能优化
- 用js根据当前季度获取上一季度
- pdf.js使用和JS实现嵌套Iframe页面F11全屏效果
- 如何用JS判断滚动条拉到了最底部
- 主流JS框架中DOMReady事件的实现
- eclipse弹出:An internal error occurred during: "JavaScript translation for 这个错误
- JavaScript 事件
- 图片上传插件ImgUploadJS:用HTML5 File API 实现截图粘贴上传、拖拽上传
- C# JSON 序列化和反序列化——JavaScriptSerializer实现
- javascript 中instanceof的用法