Вопрос по twitter-bootstrap – Используя Twitter Bootstrap, как вы можете «запомнить» действия пользователя?

3

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

<div class="alert-message success" data-alert="alert">
<a class="close" data-dismiss="alert" href="#">&times;</a>When I click &times; I'd like this to never appear again.</div>

Ваш Ответ

2   ответа
0

I've used the livequery plugin to wait for the alert elements to exist on the DOM The script is looking for an attribute data-id on the alert div, with an unique value. This will be used later for storing on the cookie or db, and can be optional. I've used the cookies plugin to store the closed alert data id, this unique value can be optional, and dinamically set as an html attribute. Even if the storing method can be improved a lot (like using json for storing multiple alerts inside one cookie as objects), this is just fine for my needs.

//bind to close alerts link
$('.alert a.close').livequery(function()
{
    $(this).bind('click', function()
    {
        var id = $(this).parents('.'+$(this).data('dismiss')).data('id');

        if(id)
        $.cookie('alert-'+id, 'closed', { path: '/' });
    });
});

//hide closed alerts
$('.alert').livequery(function()
{
    var id = $(this).data('id');

    if($.cookie('alert-'+id))
    $(this).hide();
});
4

.

// jQuery pulled from Google CDN
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
// jQuery cookie plugin. You will have to download it on your own: https://github.com/carhartl/jquery-cookie
<script src="/path/to/jquery.cookie.js"></script>
// jQuery action here
<script>
    function runOnLoad(){
        if($.cookie('alert-box') == null) {
            $.cookie('alert-box', 'open', { expires: 7 });
        } else if($.cookie('alert-box') == 'close') {
            $(".close").hide();
        }

        // php psuedo code here (if you are using the server option)
        <?php
            if(check database for hide option == true){
                echo '$(".close").hide();
            }
        ?>
    }
    function hideMe(){
        $.cookie('alert-box', 'close', {expires:7 });
        $(".close").hide();
    }
</script>

<body onload="runOnLoad()">
    <div class="alert-message success" data-alert="alert">
        <a class="close" data-dismiss="alert" href="hideMe.php" onclick="hideMe()" >&times;</a>When I click &times; I'd like this to never appear again.
    </div>
</body>

set the hide option in a table in your database, i.e. userPreference to true redirect the user back to the page that he is viewing.

I utilised jQuery's hide. Read on it. You can read more about jQuery cookie plugin here.
Error: User Rate Limit Exceeded bradm
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded bradm

Похожие вопросы