jquery cookie插件代码类

提供方便方法操作cookie :


代码如下:

$.cookie('the_cookie'); // 获得cookie
$.cookie('the_cookie', 'the_value'); // 设置cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'sosuo8.com', secure: true });
$.cookie('the_cookie', '', { expires: -1 }); // 删除
$.cookie('the_cookie', null); // 删除 cookie

代码:


代码如下:

/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

/**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};

(0)

相关推荐

  • 怎样使用php与jquery设置和读取cookies

    HTTP协议是一种无状态协议,这意味着你对网站的每一个请求都是独立的,而且因此无法通过它自身保存数据.但这种简单性也是它在互联网早期就广泛传播的原因之一. 不过,它仍然有一种方法能让你用cookies的形式来保存请求之间的信息.这种方法使你能够更有效率的进行会话管理和维持数据. 有两种处理cookies的方式-服务端(php,asp等)和客户端(javascript).在这个教程中,我们将学习到以php和javascript这两种方式如何去创建cookies. Cookies and php s

  • jQuery的cookie插件实现保存用户登陆信息

    复制代码 代码如下: <!DOCTYPE html> <html> <head> <title>cookies.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this i

  • 通过Jquery.cookie.js实现展示浏览网页的历史记录超管用

    本文就是要利用cookie插件,获取用户浏览文章历史记录,并将用户最近浏览历史记录显示在页面. 在需要添加cookie的页面加上如下js <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/jquery.cookie.js"></script

  • jquery插件如何使用 jQuery操作Cookie插件使用介绍

    代码: 复制代码 代码如下: jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options = $.extend({}, options); // clone object since i

  • jQuery控制cookie过期时间的方法

    本文实例讲述了jQuery控制cookie过期时间的方法.分享给大家供大家参考.具体分析如下: jquery.cookie()方法可以设置cookie,默认条件下是浏览器关闭cookie即失效,用下面方法可以解决这个问题 <script type="text/javascript"> var expiresDate= new Date(); expiresDate.setTime(expire.getTime() + (? * 60 * 1000)); //?替换成分钟数如

  • jQuery.cookie.js实现记录最近浏览过的商品功能示例

    本文实例讲述了jQuery.cookie.js实现记录最近浏览过的商品功能.分享给大家供大家参考,具体如下: 1.jquery.cookie.js /*jquery.cookie.js */ jquery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value

  • jquery.cookie用法详细解析

    Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是浏览器设置为启用cookie). 例如购物网站存储用户曾经浏览过的产品列表,或者门户网站记住用户喜欢选择浏览哪类新闻. 在用户允许的情况下,还可以存储用户的登录信息,使得用户在访问网站时不必每次都键入这些信息? 怎么在js/jquery中操作处理cookie那?今天分享一个cookie操作类--jQ

  • jQuery操作cookie方法实例教程

    本文实例讲述了jQuery操作cookie的方法.分享给大家供大家参考.具体方法如下: 先来看看jq.cookie的aip 写入cookie 复制代码 代码如下: $.cookie("this-cookie","this-value",{     expires:10,//有效日期     path:"/",//cookie的路 径     domanin:    //cookie的域名     secure:true //true,cookie

  • jquery使用Cookie和JSON记录用户最近浏览历史

    在一些电商网站,有"商品浏览历史记录"这一功能,一些视频类.小说类的网站也能记录用户最近的浏览历史.本文将使用Cookie以及JSON来讲解如何实现这一功能. Cookie可以用来记录客户端用户ID.密码.浏览过的网页.停留的时间等信息,jQuery提供了一个cookie插件,能非常方便的读写cookie信息. 基本流程: 1.获取文章详情页面文章的标题和页面地址: 2.获取浏览历史cookie信息,判断如果浏览历史的cookie中已经存在当前文章的浏览记录,则不进行任何操作: 3.如

  • 使用jQuery操作Cookies的实现代码

    当你浏览某网站时,你硬盘上会生产一个非常小的文本文件,它可以记录你的用户ID.密码.浏览过的网页.停留的时间等信息. 当你再次来到该网站时,网站通过读取Cookies,得知你的相关信息,就可以做出相应的动作,如在页面显示欢迎你的标语,或者让你不用输入ID.密码就直接登录等等.从本质上讲,它可以看作是你的身份证. 使用传统的Javascript来设置和获取Cookies信息很麻烦,要写上几个函数来处理,幸运的是jQuery帮我们做了很多事,借助jQuery插件,我们可以轻松的创建.获取和删除Coo

  • jquery.cookie() 方法的使用(读取、写入、删除)

    一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> <script type="text/javascript" src="

  • 基于jquery的cookie的用法

    example $.cookie('name', 'value'); 设置cookie的值,把name变量的值设为value example $.cookie('name', 'value', {expires: 7, path: '/', domain: 'jquery.com', secure: true}); 新建一个cookie 包括有效期 路径 域名等 example $.cookie('name', 'value'); 新建cookie example $.cookie('name'

  • jquery.cookie.js 操作cookie实现记住密码功能的实现代码

    复制代码 代码如下: //初始化页面时验证是否记住了密码 $(document).ready(function() { if ($.cookie("rmbUser") == "true") { $("#rmbUser").attr("checked", true); $("#user").val($.cookie("userName")); $("#pass").va

随机推荐