【问题标题】:jQuery cookie plugin creates duplicate cookies when used in different pagesjQuery cookie 插件在不同页面中使用时会创建重复的 cookie
【发布时间】:2015-07-24 12:36:25
【问题描述】:

我在两个不同页面的以下脚本中使用 jQuery Cookie 插件。我希望页面使用相同的 cookie ($.cookie("mdpage"))。但是每个页面都会创建一个新的重复 cookie。我尝试了路径变量,但它不起作用。

var refreshfix;
 matchLoad();
 
 function matchLoad(){

     
     if(typeof($.cookie("mdpage")) == "undefined" && $.cookie("mdpage") == null){
                $.cookie("mdpage",1);
            }   
     console.log($.cookie("mdpage"));
     $.ajax({
        url: baseUrl + '/matchfetch?page='+ $.cookie("mdpage"),
        data: {
            format: 'json'
        },
        beforeSend: function() {
            $('.match_list,.match_list2').html('<h6 class="text-center"><img src="'+baseUrl+'/dist/img/matchloader.gif" width="64" alt="Loading Matches..."></h6>');
            $('.ajpage').html('');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        },
        dataType: 'json',
        success: function(result) {
            
            if(result.total != 0 && result.total != "undefined"){
                
            $('.match_list,.match_list2').html('<ul> </ul>');
            $.cookie("mdpage",result.current_page);
            $.each(result.data, function(i, item) {
            var tagstring = '';
            tagstring += '<a href="'+ baseUrl +'/matches/view/'+ result.data[i].user_fb_id +'" data-toggle="popover" data-placement="bottom"><li class="user_box" > <img src="https://graph.facebook.com/'+ result.data[i].user_fb_id +'/picture?width=120&height=120&type=square" class="img-responsive">';
            
            if(result.data[i].is_in_app == 1){                          
                tagstring += '<ins></ins>';
            }
            
            tagstring += '<div class="popper-content hide"><h5>'+ result.data[i].name +'</h5></div></li></a>';
            
            $(".match_list > ul,.match_list2 > ul").append(tagstring);
            });
            
            if($.cookie("mdpage") == 1 && result.last_page != 1){
                var next_page = parseFloat($.cookie("mdpage")) + 1;
                $('.ajpage').html('<nav><ul class="pager"><li><a href="#" class="btn btn-default" disabled><i class="fa fa-chevron-left"></i></a></li><li><a href="#" class="btn btn-default" onclick="mdpageswitch('+next_page+')"><i class="fa fa-chevron-right"></i></a></li></ul></nav>');
            }
            else if(result.last_page == 1){
                $('.ajpage').html('<nav><ul class="pager"><li><a href="#" class="btn btn-default" disabled><i class="fa fa-chevron-left"></i></a></li><li><a href="#" class="btn btn-default" disabled><i class="fa fa-chevron-right"></i></a></li></ul></nav>');
            }
            else if($.cookie("mdpage") == result.last_page){
                var prev_page = parseFloat($.cookie("mdpage")) - 1;
                $('.ajpage').html('<nav><ul class="pager"><li><a href="#" class="btn btn-default" onclick="mdpageswitch('+prev_page+')"><i class="fa fa-chevron-left"></i></a></li><li><a href="#" class="btn btn-default" disabled><i class="fa fa-chevron-right"></i></a></li></ul></nav>');
            }
            else {
                var next_page = parseFloat($.cookie("mdpage")) + 1;
                var prev_page = parseFloat($.cookie("mdpage")) - 1;
                $('.ajpage').html('<nav><ul class="pager"><li><a href="#" class="btn btn-default" onclick="mdpageswitch('+prev_page+')"><i class="fa fa-chevron-left"></i></a></li><li><a href="#" class="btn btn-default" onclick="mdpageswitch('+next_page+')"><i class="fa fa-chevron-right"></i></a></li></ul></nav>');
            }
            
            $('[data-toggle="popover"]').popover({
                trigger: "hover",
                container: 'body',
                html: true,
                content: function () {
                    return $(this).find('.popper-content').html();
                }
             });
             
             $(".match_list, .match_list2, .friend_list, .match_data").niceScroll({
              cursorcolor:"#00b2de",
              cursorborder: "1px solid #00b2de",
              background: '#e6e6e6'       
              });   
              
            } 
            
            else {
                
                if(refreshfix === undefined || refreshfix === 0)
                {   
                    setTimeout(function(){
                        matchLoad();
                        refreshfix = 1;
                    }, 5000);
                }
                else{
                    $('.match_list,.match_list2').html('<h6 class="text-center">No Matches Found</h6>');
                }
            }  
            
        },
    type: 'GET'
});

 }
 
 function mdpageswitch(pageval){
     $.cookie("mdpage",pageval);
     matchLoad();
 }

【问题讨论】:

    标签: javascript php jquery cookies


    【解决方案1】:

    您需要在 cookie 上设置域:

    $.cookie('mdpage', '1', { path: '/', domain: 'example.com' });
    

    在此处查看有关如何在 jquery cookie 中使用域的更多示例:

    http://www.jquerybyexample.net/2012/06/jquery-cookies-get-set-and-delete.html

    【讨论】:

    • 你也添加了路径吗?将路径设置为“/”应该意味着 cookie 用于整个站点。您的解决方案是否使用 subdomain.example.com 之类的子域?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多