html5自带桌面通知插件,绑定点击效果直接跳转到指定页面简直完美 主要是notification.onclick = function() {window.focus();window.open(that.data.url, '_blank');}
主要代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>html5 Notification桌面通知</title>
<link href="http://yuanmouren.erlyun.com/favicon.ico" rel="shortcut icon" />
<style type="text/css">
.main{margin:0 auto;width:500px;}
input{width: 700px;height: 20px;line-height: 20px;padding: 4px 5px;border: 1px solid #e5e5e5;margin-top: 6px;border-radius: 2px;}
</style>
</head>
<body>
<div class="main">
<p>桌面提醒标题:<input type="text" id="thistitle" value="员某人提醒"/></p>
<p>桌面提醒内容:<input type="text" id="thiscontent" value="又有新的通知啦。。"/></p>
<p>点击跳转的地址:<input type="text" id="thisurl" value="http://yuanmouren.erlyun.com/"/></p>
<p><a href="javascript:;" onclick="mhcb_notify()">点我试试</a></p>
</div>
<script type="text/javascript">
function mhcb_notify() {
var title = document.getElementById("thistitle").value;
var content = document.getElementById("thiscontent").value;
var iconUrl = "http://yuanmouren.erlyun.com/assets/img/logo.png";
var gotourl = document.getElementById("thisurl").value;;
if (window.webkitNotifications) {
//chrome老版本
if (window.webkitNotifications.checkPermission() == 0) {
var notif = window.webkitNotifications.createNotification(iconUrl, title, content);
notif.display = function() {}
notif.onerror = function() {}
notif.onclose = function() {}
notif.onclick = function() {this.cancel();}
notif.replaceId = 'Meteoric';
notif.show();
closethiswindow(notif);
} else {
window.webkitNotifications.requestPermission(mhcb_notify);
}
}
else if("Notification" in window){
// 判断是否有权限
if (Notification.permission === "granted") {
var notification = new Notification(title, {
"icon": iconUrl,
"body": content,
"data": {url: gotourl}
});
closethiswindow(notification);
}
//如果没权限,则请求权限
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function(permission) {
// Whatever the user answers, we make sure we store the
// information
if (!('permission' in Notification)) {
Notification.permission = permission;
}
//如果接受请求
if (permission === "granted") {
var notification = new Notification(title, {
"icon": iconUrl,
"body": content,
"data": {url: gotourl}
});
closethiswindow(notification);
}
});
}
}
}
//mhcb_notify();
function closethiswindow(that){
that.onclick = function() {
window.focus();
window.open(that.data.url, '_blank');
}
setTimeout(function(){
if(typeof(that) !== 'undefined'){
that.close();
}
},5000);
}
</script>
</body>
</html>