国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

html5桌面通知(Web Notifications)實例解析

2020-03-24 18:38:12
字體:
來源:轉載
供稿:網友
html5桌面通知(Web Notifications)對于需要實現在新消息入線時,有桌面通知效果的情況下非常有用,在此簡單介紹一下這個html5的新屬性。這里有個不錯的demo:html5 web notification demo從上面這個demo中 我們就可以獲取所需要的基本核心代碼,如下:
復制代碼代碼如下: script
var Notification = window.Notification || window.mozNotification || window.webkitNotification;

Notification.requestPermission(function (permission) {
// console.log(permission);
});

function show() {
var instance = new Notification(
"test title", {
body: " test message"
}
);

instance.onclick = function () {
// Something to do
};
instance.onerror = function () {
// Something to do
};
instance.onshow = function () {
// Something to do
};
instance.onclose = function () {
// Something to do
};

return false;
}
/script

其中:Notification.requestPermission 這句代碼的功能就是向用戶請求權限允許。通過以上的例子,基本思路我們已經有了,首先加載文檔時,就向用戶請求權限,獲取權限后以后都so easy了。
復制代碼代碼如下:window.addEventListener('load', function () {
// At first, let's check if we have permission for notification
if (Notification Notification.permission !== "granted") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
});火狐下 驗證是通過的,但是在chrome下總是出不來,后來發現這樣一段話
復制代碼代碼如下:Not a Bug, Feature.

Desktop Notifications can only be triggered via a user action. Typing into the
JavaScript console has the same effect as raw javascript code embedded into the web
page (no user action). Typing the javascript into the location bar, however,
represents a user-action (the user is intentionally visiting a javascript link to
enable notifications, probably for sites that tend to use href="javascript:" instead
of ".

I'm pretty sure this is a non-issue.原來在chrome下是必須要用戶手動觸發的,否則,chrome瀏覽器會無視這段的js但是在我們網站里肯定不可能加一個按鈕或者超鏈接來顯式的讓用戶授權吧,好吧, 實際上這也不是個事情,我們可以在用戶經常點的按鈕上順便處理下這個授權就好,在chrome下是一次授權終身有用。除非你進入設置把他禁了。整合一下,代碼如下:
復制代碼代碼如下:function showMsgNotification(title, msg){
var Notification = window.Notification || window.mozNotification || window.webkitNotification;

if (Notification Notification.permission === "granted") {
var instance = new Notification(
title, {
body: msg,
icon: "image_url"
}
);

instance.onclick = function () {
// Something to do
};
instance.onerror = function () {
// Something to do
};
instance.onshow = function () {
// Something to do
// console.log(instance.close);
setTimeout(instance.close, 3000);
};
instance.onclose = function () {
// Something to do
};
}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, {
body: msg,
icon: "image_url"
}
);

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;
}

}html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄大仙区| 迁安市| 平潭县| 武乡县| 临泉县| 山丹县| 乌兰浩特市| 昌乐县| 仪征市| 姜堰市| 新兴县| 庐江县| 炎陵县| 沧源| 灌南县| 金门县| 儋州市| 四川省| 瓮安县| 尉犁县| 西吉县| 从化市| 石首市| 山东省| 东乡县| 洛南县| 个旧市| 龙岩市| 兰坪| 曲沃县| 重庆市| 江华| 霍山县| 长汀县| 罗山县| 隆安县| 象山县| 临洮县| 宜州市| 潮州市| 咸丰县|