jquery.cookie.js用法執行個體詳解,

來源:互聯網
上載者:User

jquery.cookie.js用法執行個體詳解,

本文執行個體講述了jquery.cookie.js用法。分享給大家供大家參考,具體如下:

對cookies的操作在當訪問一個網站就無時無刻的都伴隨著我們,記錄著我們的一舉一動,並將不危害使用者隱私的資訊,將以儲存,這樣使用者就不用去從新再次操作重複的步驟,這樣大大方便了客戶,也增加了客戶對網站的回頭率。

jquery.cookie.js 提供了jquery中非常簡單的操作cookie的方法。

$.cookie('the_cookie'); // 獲得cookie$.cookie('the_cookie', 'the_value'); // 設定cookie$.cookie('the_cookie', 'the_value', { expires: 7 }); //設定帶時間的cookie$.cookie('the_cookie', '', { expires: -1 }); // 刪除$.cookie('the_cookie', null); // 刪除 cookie$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//建立一個cookie 包括有效期間 路徑 網域名稱等

這個外掛程式預設的到期是按天數計算的,我們可以修改下,按毫秒計算,修改如下:

if (typeof options.expires === 'number') {   //var days = options.expires, t = options.expires = new Date();   //t.setDate(t.getDate() + days);   var seconds = options.expires, t = options.expires = new Date();   t.setTime(t.getTime() + seconds);   //t.setTime(t.getTime() + days);   //date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));}

下面舉個簡單的例子:我們需要對某個頁面進行閱讀統計,但是呢,在一段時間裡(比如5分鐘),同一個人無論重新整理了這個頁面多少次都好,都只能算一次。這個時候可以藉助cookie來實現:

<script language="javascript" src="/js/jquery-1.4.2.min.js"></script><script type="text/javascript" src="/js/jquery.cookie.js"></script><script language="javascript" src="/js/jquery.jsonp-2.1.4.min.js"></script><script type="text/javascript"> // 頁面類型,標識一組頁面 var pageType = 20110420; // 頁面id,標識唯一一個頁面 var url = window.location.href; var url_arr = url.split("."); var id = url_arr[url_arr.length - 2]; //var id = 2; //var cookie = $.cookie('the_cookie'+id, true, { expires: 5/24/60/60 }); $(document).ready(function(){  init_count(pageType, id); }) // 初始化資料,同一個cookie一分鐘的訪問量都算一次 function init_count(pageType, id){  if($.cookie('the_cookie'+id)){   //alert("cookie已存在");   getViewData(pageType, id);  }  else  {   // 1分鐘到期   var cookie = $.cookie('the_cookie'+id, 'Gonn', { expires: 1000 * 60 * 5 });   //$.cookie('the_cookie'+id, 'Gonn');   //var cookie = $.cookie('the_cookie'+id);   //alert(cookie);   insert_page(pageType, id);  } } // 不插入與更新時統計訪問量 function getViewData(pageType, id){  $.ajax({   type: "get",  //使用get方法訪問後台   dataType: "jsonp", //返回json格式的資料   jsonp:"callback",   url: "/manage.php", //要訪問的後台地址   data:{"opp":"view", "pageType":pageType, "id":id},   async: false,   success: function(data){    //alert(data.total);    $('#pc_1').html(data.total);    $('#pcm_1').html(data.record);   }  }) } // 插入或者更新頁面統計 function insert_page(pageType, id){  var j = null;  $.ajax({   type: "get",  //使用get方法訪問後台   dataType: "jsonp", //返回json格式的資料   jsonp:"callback",   url: "/manage.php", //要訪問的後台地址   data:{"opp":"insert", "pageType":pageType, "id":id},   async: false,   success: function(data){    //alert(msg.current);    //alert(msg.record);    j = data;    //alert("111");    //alert(j.total);    $('#pc_1').html(data.total);    $('#pcm_1').html(data.record);   }  }) }</script>

希望本文所述對大家jQuery程式設計有所協助。

您可能感興趣的文章:
  • js使用cookie記錄使用者名稱的方法
  • JS封裝cookie操作函數執行個體(設定、讀取、刪除)
  • JavaScript實現cookie的寫入、讀取、刪除功能
  • JS利用cookie記憶當前位置的防重新整理導航效果
  • javascript封裝 Cookie 應用介面
  • JavaScript對Cookie進行讀寫操作執行個體
  • 詳談javascript中的cookie
  • javascript實現設定、擷取和刪除Cookie的方法
  • js+cookies實現懸浮購物車的方法
  • js操作cookie儲存瀏覽記錄的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.