admin管理员组

文章数量:1293512

I have a bootstrap alert.

<div class="checkbox">
    <label>
        <input type="checkbox"> Don't show this again!
    </label>
</div>

If the checkbox is clicked and alert is dissmissed

<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>

I don't want to show the alert ever again. I am wondering if this can be done with .asp

$('#checkbox').click(function() {
    if ($('#checkbox').attr('checked')) {
        document.cookie="alert=dismiss";
    }
})

and then if there is a cookie hide the message $.hide method?

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 );
 }
}) 

I have a bootstrap alert.

<div class="checkbox">
    <label>
        <input type="checkbox"> Don't show this again!
    </label>
</div>

If the checkbox is clicked and alert is dissmissed

<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>

I don't want to show the alert ever again. I am wondering if this can be done with http://www.w3schools./js/js_cookies.asp

$('#checkbox').click(function() {
    if ($('#checkbox').attr('checked')) {
        document.cookie="alert=dismiss";
    }
})

and then if there is a cookie hide the message $.hide method?

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 );
 }
}) 
Share Improve this question edited Sep 13, 2017 at 1:29 nolawi asked Aug 5, 2015 at 20:56 nolawinolawi 4,6496 gold badges25 silver badges46 bronze badges 1
  • Yes - you can do it with cookies. You might want to spend a little more time on the cookie assignment code to ensure existing cookies are not overwritten. – user2182349 Commented Aug 5, 2015 at 20:59
Add a ment  | 

1 Answer 1

Reset to default 8

My remendation would be to use HTML5's local storage for this. Its very purpose is a modern way to persist data between sessions for a browser with no expiration:

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 then, when the page is ready, you would want to check and restore the functionality from a past saved experience:

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's local storage, take a look at my blog post on the topic.

本文标签: javascriptHow do I add a 39don39t show this message again39 checkbox with localstorageStack Overflow