Check out my code snippets on this example/demo page.
/* Messages Modal */
$(document).ready(function(){
var informer = $("#messageInformer a");
var refreshBadge = function(messageCount) {
var badge = informer.find(".badge");
if (messageCount > 0) {
if (!badge.length) {
informer.text("Messages ");
informer.append("<span class=\"badge\">" + messageCount + "</span>");
}
else {
badge.text(messageCount);
}
}
else {
// informer.text("No messages");
informer.text("Messages ");
informer.append("<span class=\"badge\">" + messageCount + "</span>");
}
};
var buildMessage = function(message) {
var htmlMessage = "<div class=\"alert fade in\">";
htmlMessage += "<a href=\"#\" class=\"close\" data-dismiss=\"alert\" aria-lable=\"close\" data-id=\"" + message.id + "\">×</a>";
htmlMessage += "<strong>" + message.title + "</strong>";
htmlMessage += "<p>" + message.text + "</p>";
return htmlMessage;
}
// Messages To Display
var messages = [
{
id: "1",
title: "Title 01:",
text: "<ul> \
<li>List Item</li> \
<li>List Item</li> \
</ul> \
<br/> \
<p>Paragraph</p> \
<p>E-Mail: <a href=\"mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d7dfd3dbdef2d3d6d6c0d7c1c19cd1dddf">[email protected]</a>\"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="27424a464e4b67349434542344542e40444a">[email protected]</a></a></p> \
"
},
{
id: "2",
title: "Title 02:",
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam volutpat velit et vehicula vulputate."
},
{
id: "3",
title: "Title 03:",
text: "Quisque viverra nisl ut arcu eleifend aliquam. Ut faucibus efficitur nibh, sit amet tincidunt est volutpat non."
}
];
refreshBadge(messages.length);
informer.on("click", function(e) {
e.preventDefault();
var modalBody = $(".modal-body");
modalBody.empty();
for (var i = 0; i < messages.length; i++) {
modalBody.append(buildMessage(messages[i]));
}
if (messages.length == 0) {
$('.modal-body').text('There are no more messages to display.');
}
});
$("body").delegate(".alert .close", "click", function() {
var messageId = $(this).data("id");
// AJAX
messages = messages.filter(function(el) {
return el.id != messageId;
});
if (messages.length == 0) {
// $("#messagesModal").modal("hide");
$('.modal-body').text('There are no more messages to display.');
}
refreshBadge(messages.length);
});
});
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<p data-toggle="modal" data-target="#messagesModal" id="messageInformer"><a href="#">Messages <span class="badge"></span></a>
</p>
<!-- Modal -->
<div class="modal fade" id="messagesModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Messages</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<div class="col-md-8 pull-left">
</div>
<div class="col-md-4">
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
Is there a way I can ensure the data of the modal, badge, and alerts is preserved when the page is refreshed?
For instance, if one of the three messages is deleted and the page is refreshed, only two messages should remain available.