简体   繁体   中英

possible to update cookie in javascript without page reload?

I have this code I am writing: What I am after is some info about updating the cookie (or a different approach) to accomplish the following switch without a page reload. I have about 9 different cases for this:

        switch($.cookie('page')){
            case '/':
                d = level('1','#');
                $('b').on('mouseover',function(){
                    var $this = $(this);
                    c = '#'+makeid(6);
                    n = c.search(d);
                    $this.css({'background':c,'border-radius':'0'});
                    if(n != -1){
                        $('#complete').html('<h1>Congratulations</h1>');
                        $('#disable').show();
                        $('#complete').show();
                        $('.ok').click(function(){
                            complete('1');
                        });
                    }

                });
                break;
            case '1':
                d = level('2','');
                $('b').on('mouseover',function(){
                    var $this = $(this);
                    c = '#'+makeid(6);
                    n = c.search(d);
                    $this.css({'background':c,'border-radius':'0'});
                    if(n != -1){
                        $('#complete').html('<h1>Congratulations</h1>');
                        $('#disable').show();
                        $('#complete').show();
                        $('.ok').click(function(){
                            complete('2');
                        });
                    }

                });
                break;
        }

    function complete(val){
        $('#repeat').click(function(){
            $.cookie('page','/');
            location.reload();
        });
        //$('.ok').click(function(){
            $.cookie('page',val);
        //});
        clearBoard();
        $('#ok').trigger('click');
    }

Any ideas? Would it be better to just use the alternative series of if statements?

您可以这样设置/更新cookie的值:

$.cookie('page', 'some new value');

yes you can do this

$cookie_name = 'pontikis_net_php_cookie';
$cookie_value = 'test_cookie_updated_with_php';
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), '/');

click here for more

将整个内容包装在一个函数中,并在更新cookie上调用该函数-可行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM