Skip to content

Instantly share code, notes, and snippets.

@wattry
Created January 11, 2019 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wattry/0de158db279379c7d4ba1451d06caea6 to your computer and use it in GitHub Desktop.
Save wattry/0de158db279379c7d4ba1451d06caea6 to your computer and use it in GitHub Desktop.
A simple Bootstrap alert/notification manager
/*
* A simple notification manager to handle Bootstrap alerts.
*/
/**
*
* @param {String} selector a jQuery element selector
* @param {String} alertClass success|info|danger a Bootstrap alert class.
* @param {String} message a message to be displayed in the bootstrap alert.
* @param {Boolean} append should this message should append or replace the current message.
* @param {Integer} timeout the number of seconds
*/
function setNotification(selector, alertClass, message, append, timeout) {
try {
var alertDiv = $('<div>', {
class: "alert alert-" + alertClass + " alert-dismissable fade show"
});
var alertButton = $('<button>', {
type: "button",
class: "close",
'data-dismiss': "alert",
'aria-hidden': true,
text: "×"
})
if (append) {
$(selector).append($(alertDiv).append(alertButton, message));
} else {
$(selector).html($(alertDiv).append(alertButton, message));
}
if (alertClass === "success" || timeout) {
timeout = timeout ? timeout * 1000 : 10 * 1000;
setTimeout(function () {
$(alertDiv).fadeTo(1000, 0).slideUp(1000, function () {
$(this).remove();
});
}, timeout)
}
} catch (error) {
console.log("An error occurred while setting a notification. ", error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment