jQuery 插件設置cookie
jquery.cookie.js使用介紹
2011-04-21
對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 包括有效期 路徑 域名等
這個插件默認的過期是按天數計算的,我們可以修改下,按毫秒計算,修改如下:
1 | if ( typeof options.expires === 'number' ) { |
2 | //var days = options.expires, t = options.expires = new Date(); |
3 | //t.setDate(t.getDate() + days); |
4 | var seconds = options.expires, t = options.expires = new Date(); |
5 | t.setTime(t.getTime() + seconds); |
6 | //t.setTime(t.getTime() + days); |
7 | //date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000)); |
8 | } |
下面舉個簡單的例子:我們需要對某個頁面進行閱讀統計,但是呢,在一段時間里(比如5分鐘),同一個人無論刷新了這個頁面多少次都好,都只能算一次。這個時候可以借助cookie來實現:
01 | <script language= "javascript" src= " |
02 | <script type= "text/javascript" src= " |
03 | <script language= "javascript" src= " |
04 | <script type= "text/javascript" > |
05 | // 頁面類型,標識一組頁面 |
06 | var pageType = 20110420; |
07 | // 頁面id,標識唯一一個頁面 |
08 | var url = window.location.href; |
09 | 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 | else |
25 | { |
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: " |
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: " |
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> |
代碼就直接原汁原味地貼上來吧,做個記錄。
原文:http://www.nowamagic.net/jquery/jquery_JqueryCookie.php
|
|
歡迎大家訪問我的個人網站 萌萌的IT人
posted on 2013-12-08 21:46 一堣而安 閱讀(313) 評論(0) 編輯 收藏 所屬分類: js_css