1
2
3
4
5
6
7
|
<script type=
"text/javascript"
src=
" jquery_1.4.2.min.js "
></script>
<script type=
"text/javascript"
src=
" jquery.cookie.js "
></script>
<script>
$(document).ready(
function
(){
$.cookie(
'the_cookie'
,
'the_value'
, { expires: 1 });
//1天
})
</script>
|
以天为单位确实很不方便,这里处理了下,以毫秒为单位。
1
2
3
4
5
6
7
8
9
|
<script type=
"text/javascript"
src=
" jquery_1.4.2.min.js"
></script>
<script type=
"text/javascript"
src=
" jquery.cookie.js"
></script>
<script>
$(document).ready(
function
(){
var
expireDate =
new
Date();
expireDate.setTime( expireDate.getTime() + ( 1 * 60 * 60 * 1000 ) );
//以【毫秒】为单位。这里设置的是1小时
$.cookie(
'the_cookie'
,
'the_value'
, { expires: expireDate });
})
</script>
|
jquery.cookie.js
一个轻量级的cookie 插件,可以读取、写入、删除 cookie。
使用方法:
1.新添加一个会话 cookie:
$.cookie('the_cookie', 'the_value');
注:当没有指明 cookie有效时间时,所创建的cookie有效期默认到用户关闭浏览器为止,所以被称为
“会话cookie(session cookie)”。
2.创建一个cookie并设置有效时间为 7天:
$.cookie('the_cookie', 'the_value', { expires: 7 });
注:当指明了cookie有效时间时,所创建的cookie被称为“持久 cookie (persistent cookie)”。
3.创建一个cookie并设置 cookie的有效路径:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
注:在默认情况下,只有设置 cookie的网页才能读取该 cookie。如果想让一个页面读取另一个页面设
置的cookie,必须设置cookie的路径。cookie的路径用于设置能够读取 cookie的顶级目录。将这
个路径设置为网站的根目录,可以让所有网页都能互相读取 cookie (一般不要这样设置,防止出现冲突)。
4.读取cookie:
$.cookie('the_cookie'); // cookie存在 => 'the_value'
$.cookie('not_existing'); // cookie不存在 => null
5.删除cookie,通过传递null作为cookie的值即可(注意要加上路径):
$.cookie('the_cookie', null, { path: '/' });
----------相关参数的解释---------------
1).expires: 365
定义cookie的有效时间,值可以是一个数字(从创建cookie时算起,以天为单位)或一个Date 对象。如果省略,那么创建的cookie是会话cookie,将在用户退出浏览器时被删除。
2).path: '/'
默认情况:只有设置cookie的网页才能读取该cookie。
定义cookie的有效路径。默认情况下, 该参数的值为创建 cookie 的网页所在路径(标准浏览器的行为) 。
如果你想在整个网站中访问这个cookie需要这样设置有效路径:path: '/'。如果你想删除一个定义
了有效路径的 cookie,你需要在调用函数时包含这个路径:$.cookie('the_cookie', null,
{ path: '/' });。 domain: 'example.com'
默认值:创建 cookie的网页所拥有的域名。
3).secure: true
默认值:false。如果为true,cookie的传输需要使用安全协议(HTTPS)。
4).raw: true
默认值:false。
默认情况下,读取和写入 cookie 的时候自动进行编码和解码(使用encodeURIComponent 编码,
decodeURIComponent 解码)。要关闭这个功能设置 raw: true 即可。
以下网上摘取:
对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
2
3
4
5
6
7
8
|
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
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<script language=
"javascript"
src=
"http://www.nowamagic.net/zt/access_count/js/jquery-1.4.2.min.js"
></script>
<script type=
"text/javascript"
src=
"http://www.nowamagic.net/zt/access_count/js/jquery.cookie.js"
></script>
<script language=
"javascript"
src=
"http://www.nowamagic.net/zt/access_count/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:
"http://www.nowamagic.net/zt/access_count/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:
"http://www.nowamagic.net/zt/access_count/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>
|
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1357909,如需转载请自行联系原作者