Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active August 29, 2015 14:22
Show Gist options
  • Save yratof/47d49394772e547ed765 to your computer and use it in GitHub Desktop.
Save yratof/47d49394772e547ed765 to your computer and use it in GitHub Desktop.
Notifications when you click on contact page...
// Notifications – LOL
window.addEventListener('load', function () {
// At first, let's check if we have permission for notification
// If not, let's ask for it
if (window.Notification && Notification.permission !== "granted") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
});
}
var button = document.getElementsByTagName('div')[0];
var message = "Want a quote? Call us XXXXX XXXXXX";
button.addEventListener('click', function () {
// If the user agreed to get notified
if (window.Notification && Notification.permission === "granted") {
var n = new Notification(message);
}
// If the user hasn't told if he wants to be notified or not
// Note: because of Chrome, we are not sure the permission property
// is set, therefore it's unsafe to check for the "default" value.
else if (window.Notification && Notification.permission !== "denied") {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
// If the user said okay
if (status === "granted") {
var n = new Notification(message);
}
// Otherwise, we can fallback to a regular modal alert
else {
alert(message);
}
});
}
// If the user refuses to get notified
else {
// We can fallback to a regular modal alert
alert(message);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment