我找到了以下 HTML 通知示例,它在 Chrome 和 Firefox 中运行良好。下载并在本地试用后,它不再适用于 Chrome。这是预期的行为(Chrome 出于某种原因在本地阻止通知)还是有任何其他原因导致它不起作用?
<!DOCTYPE html>
<html>
<body>
<button onclick="notifyMe()">Notify me!</button>
<script>
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check if the user is okay to get some notification
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
// Note, Chrome does not implement the permission static property
// So we have to check for NOT 'denied' instead of 'default'
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 the user is okay, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}
</script>
</body>
</html>
最佳答案
根据W3C Specification只要文件托管在某处,您所拥有的看起来就没问题。这是一个不错的 notification repo , 与 live demo .
关于javascript - HTML 5 通知在 Chrome 中无法在本地运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24492107/