Jquery.cookie使用
来源:互联网 发布:淘宝网上的汽车能买吗 编辑:程序博客网 时间:2024/05/29 10:13
对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=
"http://www.nowamagic.net/zt/access_count/js/jquery-1.4.2.min.js"
></script>
02
<script type=
"text/javascript"
src=
"http://www.nowamagic.net/zt/access_count/js/jquery.cookie.js"
></script>
03
<script language=
"javascript"
src=
"http://www.nowamagic.net/zt/access_count/js/jquery.jsonp-2.1.4.min.js"
></script>
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:
"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>
代码就直接原汁原味地贴上来吧,做个记录。
0 0
- 使用Jquery.cookie实现cookie
- jquery cookie使用
- jquery.cookie.js使用
- Jquery.cookie使用
- jquery.cookie.js使用
- jquery.cookie的使用
- jquery.cookie 使用介绍
- Jquery.Cookie的使用
- jquery.cookie.js使用
- jquery.cookie.js使用
- jquery.cookie()的使用
- jquery-cookie 使用 详解
- 使用cookie(需要引入jquery.cookie.js)
- 关于使用jquery.cookie.js 操作cookie
- JQuery的Cookie插件使用
- jquery.cookie.js的使用
- jquery操作Cookie插件使用
- jquery.cookie.js使用介绍
- Codeforces 388A Fox and Box Accumulation(贪心)
- Linux-2.6.32.2内核在mini2440上的移植(四)---根文件系统制作(1)
- leetCode刷题笔记(六)
- bzoj2743 HEOI2012采花 离线预处理
- C语言文件操作函数大全
- Jquery.cookie使用
- Socks代理反弹突破内网
- 第五周-基本控制语句
- JAVA 回调实例
- 爱的另一种诠释
- 高精度除法
- c++ 中的 new ,opertaor new和placement new
- 工具类代码
- Asp.net动态生成Word文档并填充数据 转