jquery.cookie.js使用介紹

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   使用   java   

對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來實現:

 1 <script language="javascript" src="http://www.nowamagic.net/zt/access_count/js/jquery-1.4.2.min.js"></script> 2 <script type="text/javascript" src="http://www.nowamagic.net/zt/access_count/js/jquery.cookie.js"></script> 3 <script language="javascript" src="http://www.nowamagic.net/zt/access_count/js/jquery.jsonp-2.1.4.min.js"></script> 4 <script type="text/javascript"> 5     // 頁面類型,標識一組頁面 6     var pageType = 20110420; 7     // 頁面id,標識唯一一個頁面 8     var url = window.location.href; 9     var url_arr = url.split(".");10     var id = url_arr[url_arr.length - 2];11     //var id = 2;12     //var cookie = $.cookie(‘the_cookie‘+id, true, { expires: 5/24/60/60 });13         14     $(document).ready(function(){15         init_count(pageType, id);16     })17         18     // 初始化資料,同一個cookie一分鐘的訪問量都算一次19     function init_count(pageType, id){20         if($.cookie(‘the_cookie‘+id)){21             //alert("cookie已存在");22             getViewData(pageType, id);23         }24         else25         {26             // 1分鐘到期27             var cookie = $.cookie(‘the_cookie‘+id, ‘Gonn‘, { expires: 1000 * 60 * 5 });28             //$.cookie(‘the_cookie‘+id, ‘Gonn‘);29             //var cookie = $.cookie(‘the_cookie‘+id);30             //alert(cookie);31             insert_page(pageType, id);32             33         }34     }35     36     // 不插入與更新時統計訪問量37     function getViewData(pageType, id){38         $.ajax({  39             type: "get",        //使用get方法訪問後台  40             dataType: "jsonp",    //返回json格式的資料  41             jsonp:"callback",42             url: "http://www.nowamagic.net/zt/access_count/manage.php",    //要訪問的後台地址  43             data:{"opp":"view", "pageType":pageType, "id":id},44             async: false,45             success: function(data){  46                 //alert(data.total);47                 $(‘#pc_1‘).html(data.total);48                 $(‘#pcm_1‘).html(data.record);49             }  50         })  51     }52     53     // 插入或者更新頁面統計54     function insert_page(pageType, id){55         var j = null;56         $.ajax({  57             type: "get",        //使用get方法訪問後台  58             dataType: "jsonp",    //返回json格式的資料  59             jsonp:"callback",60             url: "http://www.nowamagic.net/zt/access_count/manage.php",    //要訪問的後台地址  61             data:{"opp":"insert", "pageType":pageType, "id":id},62             async: false,63             success: function(data){  64                 //alert(msg.current);65                 //alert(msg.record);66                 j = data; 67                 //alert("111");68                 //alert(j.total);69                 $(‘#pc_1‘).html(data.total);70                 $(‘#pcm_1‘).html(data.record);71             }  72         })  73     }74 75 </script>

操作cookie實現“記住密碼”,很簡單很強大

//初始化頁面時驗證是否記住了密碼$(document).ready(function() {    if ($.cookie("rmbUser") == "true") {        $("#rmbUser").attr("checked", true);        $("#user").val($.cookie("userName"));        $("#pass").val($.cookie("passWord"));    }});//儲存使用者資訊function saveUserInfo() {    if ($("#rmbUser").attr("checked") == true) {        var userName = $("#user").val();        var passWord = $("#pass").val();        $.cookie("rmbUser", "true", { expires: 7 }); // 儲存一個帶7天期限的 cookie        $.cookie("userName", userName, { expires: 7 }); // 儲存一個帶7天期限的 cookie        $.cookie("passWord", passWord, { expires: 7 }); // 儲存一個帶7天期限的 cookie    }    else {        $.cookie("rmbUser", "false", { expires: -1 });        $.cookie("userName", ‘‘, { expires: -1 });        $.cookie("passWord", ‘‘, { expires: -1 });    }}

cookie本質上是一個txt文本,因此只能夠存入字串,對象通常要序列化之後才能存入cookie,而取的時候要反序列才又能得到對象。

 1 $(function () { 2             if ($.cookie("o") == null) { 3                 var o = { name: "張三", age: 24 }; 4                 var str = JSON.stringify(o);  //對序列化成字串然後存入cookie 5                 $.cookie("o", str, { 6                     expires:7   //設定時間,如果此處留空,則瀏覽器關閉此cookie就失效。 7                 }); 8                 alert("cookie為空白"); 9             }10             else {11                 var str1 = $.cookie("o");12                 var o1 = JSON.parse(str1);  //字元還原序列化成對象13                 alert(o1.name);        //輸還原序列化出來的對象的姓名值14             }15         })

 

   

jquery.cookie.js使用介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.