Depois de examinar algumas das respostas aqui e em outro tópico, aqui está o que eu terminei:
Criei uma função chamada showAlert()que adicionaria dinamicamente um alerta, com um opcional typee closeDealy. Para que você possa, por exemplo, adicionar um alerta do tipo danger(ou seja, alerta-perigo do Bootstrap) que fechará automaticamente após 5 segundos, assim:
showAlert("Warning message", "danger", 5000);
Para isso, adicione a seguinte função Javascript:
function showAlert(message, type, closeDelay) {
if ($("#alerts-container").length == 0) {
// alerts-container does not exist, add it
$("body")
.append( $('<div id="alerts-container" style="position: fixed;
width: 50%; left: 25%; top: 10%;">') );
}
// default to alert-info; other options include success, warning, danger
type = type || "info";
// create the alert div
var alert = $('<div class="alert alert-' + type + ' fade in">')
.append(
$('<button type="button" class="close" data-dismiss="alert">')
.append("×")
)
.append(message);
// add the alert div to top of alerts-container, use append() to add to bottom
$("#alerts-container").prepend(alert);
// if closeDelay was passed - set a timeout to close the alert
if (closeDelay)
window.setTimeout(function() { alert.alert("close") }, closeDelay);
}