
當想要記住瀏覽者的一些客制化的設定時,除了使用資料庫來記錄之外,如果並不是很特別重要的設定的話,或者可以考慮使用 aqCookie 的方式來記錄。
套件名稱:aqCookie
套件版本:1.0
作者網站:http://aquaron.com/~jquery/aqCookie
套件網址:http://plugins.jquery.com/project/aqCookie
發佈日期:2008-10-31
檔案大小:1.01 KB
檔案下載:aqCookie.js
方法說明:
檢視原始碼 JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // 設定 cookie;若沒給 value 則表示刪除該 cookie $.aqCookie.set(name, value); // 取得 cookie;不管是只給 name 或是陣列都會回傳一個陣列值,其中 name 是索引值 $.aqCookie.get(name | [array]); // 刪除 cookie;也可以用 set(name) 的方式 $.aqCookie.del(name); // 使用正規表示法的方式來取得 cookie 值;其中的索引值為符合的名稱 $.aqCookie.all(regex filter); // 設定 domain 值;預設是 '' $.aqCookie.domain = string; // 設定 cookie 多久過期;預設是 3153600000(約36.5天...) $.aqCookie.secToExpire = number; |
使用範例:
檢視原始碼 JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="aqCookie.js"></script> <style type="text/css"> .brownbox { width: 160px; } </style> <script type="text/javascript"> $(function(){ // 設定 cookie $(":button[value=Set cookie]").click(function(){ var _key = $(":text[name=key]").val(); if(!_key) return false; $.aqCookie.set('__'+_key, $(":text[name=val]").val()); return false; }); // 取得 cookie $(":button[value=Get cookie]").click(function(){ var _gkey = $(":text[name=gkey]").val(); var _arry = $.aqCookie.get('__'+_gkey); alert(_arry['__'+_gkey]); return false; }); // 刪除 cookie $(":button[value=Get cookie]").click(function(){ var arry = $.aqCookie.del('__'+$(":text[name=gkey]").val()); return false; }); }); </script> <body> <div class="brownbox"> <input class="txt" type="text" name="key"> <input class="txt" type="text" name="val"> <input class="btn" type="button" value="Set cookie"> </div> <div class="brownbox"> <input class="txt" type="text" name="gkey"> <input class="btn" type="button" value="Get cookie"> <input class="btn" type="button" value="Delete"> </div> </body> |