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="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>
转自: http://www.nowamagic.net/jquery/jquery_JqueryCookie.php
分享到:
相关推荐
Jquery操作Cookie
jquery操作cookie所需js包cookiejs-cookie
jQuery操作cookie的插件,大概的使用方法如下$.cookie('the_cookie'); //读取Cookie值$.cookie(’the_cookie’, ‘the_value’); //设置cookie的值$.cookie(’the_cookie’, ‘the_value’, {expires ‘/’, domain ...
Jquery操作Cookie|多个输入框同时输入效果|下拉列表经典例子
js操作Cookie子键+Jquery调用Ajax方法执行后台操作 返回值给前台。详细注释 Demo
Cookie操作是我们在Web开发中经常会用到的功能,以往我们一般是通过javascript实现的。下面这款jQuery插件是专门用来进行cookie操作的,包括cookie的添加、清除、读取。
jquery.cookie.js是一个基于jquery的插件,一个轻量级的cookie 插件,可以读取、写入、删除 cookie。接下来通过本文给大家介绍Jquery操作cookie记住用户名,需要的朋友参考下吧
jquery cookie.js 官方下载,一款优秀的 jquery 插件,提供了非常轻量级、简单、实用的操作 cookie 的方法,包括读写、删除等操作,jquery cookie 路径,jquery cookie 时间,jquery cookie 有效期,jquery cookie ...
本文实例讲述了jQuery操作cookie的方法。分享给大家供大家参考。具体方法如下: 先来看看jq.cookie的aip 写入cookie 代码如下:$.cookie(“this-cookie”,”this-value”,{ expires:10,//有效日期 path:”/”,//...
jquery操作cookie必备js包,是个很好的cookie插件.
JQuery对cookie操作。可判断是否存在cookie,可对cookie进行新建,删除,是否存在进行判断。
jQuery操作cookie的插件,大概的使用方法如下 $.cookie( the_cookie ) //读取Cookie值 $.cookie(’the_cookie’, ‘the_value’) //设置cookie的值 $.cookie(’the_cookie’, ‘the_value’, {expires: 7, path: ‘/...
使用JQuery操作cookie时 发生取的值不正确的问题: 结果发现cookie有四个不同的属性: 名称,内容,域,路径 代码如下: $.cookie(‘the_cookie’); // 读取 cookie $.cookie(‘the_cookie’, ‘the_value’); // 存储...
cookies 是一个强大的 jQuery 用来操作 Cookie 的插件。除了常见的操作 $.cookies.set( 'sessid', 'dh3tr62fghe' ); var sessid = $.cookies.get( 'sessid' ); $.cookies.del( 'sessid' ); if( $.cookies.test() ) ...