2015-05-13 82 views
0

我在兩個不同頁面的下面腳本中使用Jquery Cookie Plugin。我希望頁面使用相同的cookie($ .cookie(「mdpage」))。但每個頁面都會創建一個新的重複cookie。我嘗試了路徑變量,但它不起作用。有人給我一些建議,請。Jquery cookie插件在不同頁面中使用時創建重複的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(); 
} 

回答

0

您需要設置一個域名上的Cookie:

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

查看如何在這裏jQuery的餅乾使用域例子:

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

+0

試了一下。但問題仍然沒有解決...... – winnyboy5

+0

您是否也添加了路徑?設置到'/'的路徑應該表示該cookie用於整個站點。您的解決方案是否使用subdomain,如subdomain.example.com? – JohnMalkowich

相關問題