Jquery在页面加载时获取桌面通知
Jquery get Desktop Notification when page loaded
我使用这个代码通过点击div
来获得chrome上的桌面通知<script>
function notify() {
var havePermission = window.webkitNotifications.checkPermission();
if (havePermission == 0) {
// 0 is PERMISSION_ALLOWED
var notification = window.webkitNotifications.createNotification(
'https://i.stack.imgur.com/dmHl0.png',
'Chrome notification!',
'Here is the notification text'
);
notification.onclick = function () {
window.open("http://stackoverflow.com/a/13328397/1269037");
notification.close();
}
notification.show();
} else {
window.webkitNotifications.requestPermission();
}
}
</script>
<div style="width: 300px; height: 300px; background: yellow" onclick="notify()">
Cick here to notify
</div>
但当我把onclick
改为onload
时,不工作
我使用jquery也在这段代码,甚至使用click
事件或ready
这有什么问题吗?
我找到答案了我用这个
<script type="text/javascript">
var DesktopNotifications = {
/**
* Checks if notifications are supported
* @return {Boolean}
*/
isSupported:function() {
return (window.webkitNotifications != 'undefined')
},
/**
* ask use to display desktop notifications
* @param callback
*/
requestPermission:function(callbck) {
window.webkitNotifications.requestPermission(function() {
if (typeof(callbck) == "function") {
callbck(window.webkitNotifications.checkPermission() == 0);
}
});
},
/**
* display a notification
* @param img full path of image to be displayed e.g. http://somedomain.com/photo.jpg
* @param notitification_title title of notification
* @param notification_body body of nitification
* @return {Boolean}
*/
doNotify:function(img,notitification_title,notification_body) {
// permission is ok
if (window.webkitNotifications.checkPermission() == 0) {
window.webkitNotifications.createNotification(img, notitification_title, notification_body).show();
return true;
}
return false;
}
}
$(document).ready(function() {
// request permission to display notifications
$("#permission_request").click(function() {
DesktopNotifications.requestPermission();
});
// craete notification
$("#create_notification").ready(function() {
if (!DesktopNotifications.doNotify("https://twimg0- a.akamaihd.net/profile_images/2647445933/2c75afbe419bc7aaae71f01b29062b84_normal.jpeg",
"Desktop Notification Example",
"This is description of out Hello World notification example.")) {
alert('Unable to fire notifications. Click "Request Permission" button to allow jaspreetchahal.org/examples access to send notifications to your desktop. Please note that this is just a demo and you can block this access anytime later');
}
});
});
</script>
和
<a href="#requestnotifications" id="permission_request">Request permission</a>
<a href="#createnotification" id="create_notification">Send</a>
相关文章:
- 从桌面读取python文件时高亮显示代码
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jquery试图按名称获取按钮位置
- 如何在jQuery中获取元素的形式
- 如何在php文件中获取$.post-ajax传递的值
- 在Shopify中获取博客文章的图片
- 使用Javascript获取所选选项ID
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- 在动态创建的元素上获取对特定选择器的引用
- 从城市名称获取惊喜
- Angular只从数组中获取所需的数据
- 无法将数据从firebase获取到我的html页面
- 从ajax请求中获取javascript对象
- 如何从画布上的某个移动事件中获取X和Y
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何从Web JavaScript应用程序获取桌面C#程序中的变量
- Javascript桌面浏览器从摄像头获取头像
- 如何在Chrome桌面捕获中获取调整大小事件
- Jquery在页面加载时获取桌面通知