google chrome的确是个好东西,除了速度快,支持HTML5,还有一些新的东西,比如今天我要提到的Notification,也就是桌面提醒。
什么是桌面提醒就不用多解释了,简而言之就是在桌面右下角弹出一个对话框,给用户一些提示信息,类似于我们常见的广告。有人开玩笑说,这个Notification就是web2.0下的新广告。哈!
废话少说,我们先上图上真相,然后上代码。

<!doctype html>
<html>
<head>
<title>Notification</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'></meta>
</head>
<body>
<button onclick="notify()">Click Me to Notify</button>
<script type="text/javascript">
function notify(){
if(window.webkitNotifications){
if (window.webkitNotifications.checkPermission() > 0) {
RequestPermission(notify);
} else {
var notification=webkitNotifications.createNotification(
'http://images.cnblogs.com/cnblogs_com/flyingzl/268702/r_1.jpg',
'twitter',
'A dog is seating int the manager!'
);
//var notification=webkitNotifications.createHTMLNotification('http://www.cnblogs.com/flyingzl');
notification.show();
}
}
}
function RequestPermission(callback) {
window.webkitNotifications.requestPermission(callback);
}
</script>
</body>
</html>
简单解释下。要想弹出窗口提示需要分两步走。第一步就是要获得权限,即通知用户是否允许桌面提示。第二步就是 如果获得到了权限,就弹出提示窗口,否则不做任何操作。如果直接越过第一步,会出现诸如"Uncaught Error: SECURITY_ERR: DOM Exception 18"等异常。
浙公网安备 33010602011771号