Created
October 22, 2013 00:48
-
-
Save JessieAMorris/7093460 to your computer and use it in GitHub Desktop.
HTML5 Notifications demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style type="text/css"> | |
.hide { | |
display: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="create-notification-pane hide"> | |
<button id="createNotification">Create A Basic Notification</button> | |
</div> | |
<div class="not-supported-pane"> | |
Sorry, but your browser isn't supported. You should upgrade to either Firefox or Chrome. | |
</div> | |
<script type="text/javascript"> | |
if(typeof window.Notification !== 'undefined') { | |
document.querySelector(".not-supported-pane").classList.add("hide"); | |
document.querySelector(".create-notification-pane").classList.remove("hide"); | |
} | |
function createNotification(){ | |
if(Notification) { | |
if(Notification.permission === "granted") { | |
var notification = new Notification("Hello, world!"); | |
} else if(Notification.permission !== "denied") { // We only want to do this if the user hasn't explicitly said no | |
Notification.requestPermission(function(permission) { | |
// Chrome doesn't store this, so let's do that for them. | |
if(!Notification.permission || Notification.permission !== permission) { | |
Notification.permission = permission; | |
} | |
if(Notification.permission === "granted") { | |
createNotification(); | |
} | |
}); | |
} | |
} else { | |
document.querySelector(".not-supported-pane").classList.remove("hide"); | |
} | |
} | |
var button = document.querySelector("#createNotification"); | |
button.addEventListener("click", createNotification); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment