How do I add a "Don't show this message again" checkbox from localstorage?
I have a bootstrap alert.
<div class="checkbox">
<label>
<input type="checkbox"> Don't show this again!
</label>
</div>
If this checkbox is clicked and the alert is disabled
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
I don't want to show the warning anymore. I am wondering if this can be done with http://www.w3schools.com/js/js_cookies.asp
$('#checkbox').click(function() {
if ($('#checkbox').attr('checked')) {
document.cookie="alert=dismiss";
}
})
and then if there is a cookie, hide the post method $.hide
?
Updated with solution
var oldBrowser = $.browser.msie && parseInt($.browser.versionNumber) < 9;
//message is dismissed via storage
var dissmissed = amplify.store("DoNotShowMessageAgain");
// if oldbrowers and not dismissed
if (oldBrowser && !dissmissed) {
$( "#browserAlert" ).show();
}
// Don't show message again
$('#browserAlert .close').click(function(){
if ($('.checkbox input').is(':checked')) {
amplify.store( "DoNotShowMessageAgain", true );
}
})
+3
source to share
1 answer
My recommendation would be to use HTML5 storage for this. Its purpose is a modern, non-expiring way of persisting data between sessions for the browser:
function localStorageAvailable() {
if (typeof(Storage) !== "undefined") {
return true;
}
else {
return false;
}
}
$('#checkbox').click(function(){
if ($('#checkbox').attr('checked')) {
if (localStorageAvailable())
localStorage.DoNotShowMessageAgain = "true";
}
})
And then, when the page is ready, you want to test and restore functionality from past saved experiences:
if (localStorageAvailable()) {
if (localStorage.DoNotShowMessageAgain && localStorage.DoNotShowMessageAgain === "true") {
// user doesn't want to see the message again, so handle accordingly
}
};
For more information on HTML5 local storage check out my blog post on the topic.
+4
source to share