Jquery cookie插件在不同页面

时间:2015-05-13 09:45:44

标签: javascript php jquery cookies

我在下面的脚本中使用Jquery Cookie Plugin在两个不同的页面中。我希望页面使用相同的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();
 }

1 个答案:

答案 0 :(得分:0)

您需要在Cookie上设置域名:

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

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

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