石家庄网站建设公司,网站优化,400电话办理,企业邮箱——明尚互联,石家庄做网站首选

石家庄网站建设公司,网站优化,400电话办理,企业邮箱-明尚互联 石家庄做网站首选

网站建设服务热线
因为专业,所以与众不同

jQuery插件之jQuery操作Cookie的插件使用方法


石家庄明尚互联网络公司 发布于:2011-03-08 06:24:14  点击:2900  字体:
以下是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.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
        }
        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;
    }
};

使用方法
jQuery操作cookie的插件,大概的使用方法如下:
$.cookie(’the_cookie’); //读取Cookie值
$.cookie(’the_cookie’, ’the_value’); //设置cookie的值
$.cookie(’the_cookie’, ’the_value’, {expires: 7, path: ’/’, domain: ’www.viming.com’, secure: true});//新建一个cookie 包括有效期 路径 域名等
$.cookie(’the_cookie’, ’the_value’); //新建cookie
$.cookie(’the_cookie’, null); //删除一个cookie

设置一个名称为weburl,值为www.viming.com的cookie:
$.cookie("weburl", "www.viming.com");

设置一个名称为weburl,值为www.viming.com的cookie,同时设置过期时间(expires属性)为7天:
$.cookie("weburl", "www.viming.com", {expires:7});

设置一个名称为weburl,值为www.viming.com的cookie,设置过期时间(expires属性)为7天,同时设置cookie的path属性为”/admin”
$.cookie("weburl", "www.viming.com", {path:’/admin’, expires:7});

读取名称为weburl的cookie值:
alert($.cookie("weburl"));

删除名称为weburl的cookie:
$.cookie("weburl", null);

https://www.hiheb.cn/
最新案例
建站套餐
明尚最新动态
手机:19033292827
首页  套餐  服务  案例  关于  留言  联系  新闻
返回头部
联系我们
Contact
QQ咨询623900980  QQ咨询81457951
业务咨询:19033292827
地址:河北省石家庄市长安区古城西路汇春博物园
Copyright © 2007-2024 石家庄明尚信息技术有限公司
备案编号:冀ICP备12016215号-2 冀公网安备13010502002383号
业务范围:石家庄网站建设 石家庄网站优化 石家庄网站制作 石家庄做网站 石家庄建网站 石家庄建站 石家庄网页设计