jquery.cookie.js用法实例详解
作者:bea
本文实例讲述了jquery.cookie.js用法。分享给大家供大家参考,具体如下: 对cookies的操作在当访问一个网站就无时无刻的都伴随着我们,记录着我们的一举一动,并将不危害用户隐私的信息,将以保存,这样用户就不用去从新再次操作重复的步骤,这样大大方便了客户,也增加了客户对网站的回头率。 jquery.cookie.js 提供了jquery中非常简单的操作cookie的方法。 $.cookie('the_cookie'); // 获得cookie$.cookie(
本文实例讲述了jquery.cookie.js用法。分享给大家供大家参考,具体如下:
对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 包括有效期 路径 域名等
这个插件默认的过期是按天数计算的,我们可以修改下,按毫秒计算,修改如下:
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来实现:
<script language="javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/js/jquery.cookie.js"></script>
<script language="javascript" src="/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: "/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: "/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>
希望本文所述对大家jQuery程序设计有所帮助。
有用 | 无用
对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 包括有效期 路径 域名等
这个插件默认的过期是按天数计算的,我们可以修改下,按毫秒计算,修改如下:
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来实现:
<script language="javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/js/jquery.cookie.js"></script>
<script language="javascript" src="/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: "/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: "/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>
希望本文所述对大家jQuery程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 认识Knockout及如何使用Knockout绑定上下文
- 详解Document.Cookie
- 不得不分享的JavaScript常用方法函数集(下)
- JQuery datepicker 用法详解
- 一道JS前端闭包面试题解析
- 干货分享:让你分分钟学会javascript闭包
- javascript生成img标签的3种实现方法(对象、方法、html)
- 谈谈我对JavaScript中typeof和instanceof的深入理解
- JavaScript中Window对象的属性及事件
- JavaScript字符串删除重复字符的方法
- JavaScript如何实现在文本框(密码框)输入提示语
- jquery实现图片预加载
- 基于jquery实现图片相关操作(重绘、获取尺寸、调整大小、缩放)
- 使用堆实现Top K算法(JS实现)
- 原生js和jQuery实现淡入淡出轮播效果
- jQuery实现模仿微博下拉滚动条加载数据效果
- 尝试动手制作javascript放大镜效果
- js操作cookie保存浏览记录的方法
- js实现跨域的多种方法