詳解jQuery的Cookie外掛程式_jquery

來源:互聯網
上載者:User

一、jQuery.Cookie.js外掛程式是一個輕量級的Cookie管理外掛程式。

  特別提醒,今日發現一個特別的錯誤,google瀏覽器提示:has no method $.cookie。Firefox瀏覽器提示:$.cookie is not a function;調試了半天,終於找到原因,如果同一個頁面兩次或者多次引入jQuery外掛程式就會報此錯誤。

  使用方法:

  1、引入jQuery與jQuery.Cookie.js外掛程式。

 <script src="jQuery.1.8.3.js" type="text/javascript"></script> <script src="jquery.cookie.js" type="text/javascript"></script>

  2、函數。

   文法:$.cookie(名稱,值,[option])

   (1)讀取cookie值

   $.cookie(cookieName)         cookieName:要讀取的cookie名稱。

樣本:$.cookie("username");      讀取儲存在cookie中名為的username的值。

   (2)寫入設定Cookie值:

   $.cookie(cookieName,cookieValue);  cookieName:要設定的cookie名稱,cookieValue表示相對應的值。 

樣本: $.cookie("username","admin");  將值"admin"寫入cookie名為username的cookie中。      $.cookie("username",NULL);   銷毀名稱為username的cookie

   (3) [option]參數說明:

     expires:  有限日期,可以是一個整數或一個日期(單位:天)。  這個地方也要注意,如果不設定這個東西,瀏覽器關閉之後此cookie就失效了

     path:    cookie值儲存的路徑,預設與建立頁路徑一致。

       domin:    cookie網域名稱屬性,預設與建立頁網域名稱一樣。  這個地方要相當注意,跨域的概念,如果要主網域名稱次層網域有效則要設定  ".xxx.com"

       secrue:   一個布爾值,表示傳輸cookie值時,是否需要一個安全性通訊協定。

 樣本: $.cookie("like", $(":radio[checked]").val(), {    path: "/", expiress: 7  })

一個完整設定與讀取cookie的頁面代碼:

<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>jQuery學習2</title> <script src="jQuery.1.8.3.js" type="text/javascript"></script> <script src="jquery.cookie.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#username").val($.cookie("username")); if ($.cookie("like") == "劉德華") { $(":radio[value='劉德華']").attr("checked", 'checked') } else { $(":radio[value='張學友']").attr("checked", 'checked') } $(":button").click(function () { $.cookie("username", $("#username").val(), {  path: "/", expires: 7 }) $.cookie("like", $(":radio[checked]").val(), {  path: "/", expiress: 7 }) }) }) </script></head><body> <p><input type="text" id="username" value="" /></p> <p> <input type="radio" name="like" value="劉德華" />劉德華 <input type="radio" name="like" value="張學友" />張學友 </p> <p><input type="button" value="儲存" /></p></body></html>

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

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

以上就是本文的全部內容,希望對大家有所協助,謝謝對雲棲社區的支援!

聯繫我們

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