`
wenjinglian
  • 浏览: 812403 次
  • 性别: Icon_minigender_1
  • 来自: 株洲->深圳
社区版块
存档分类
最新评论

jquery.cookie.js 1.4源码

阅读更多

 

Jquery cookie plugin v1.4.0 源码:https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js

 

/*!
 * jQuery Cookie Plugin v1.4.0
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2013 Klaus Hartl
 * Released under the MIT license
 */
(function (factory) {    //AMD 模块加载
        if (typeof define === 'function' && define.amd) {
                // AMD. Register as anonymous module.
                define(['jquery'], factory);
        } else {
                // Browser globals.
                factory(jQuery);
        }
}(function ($) {

        var pluses = /\+/g;

        function encode(s) {
                return config.raw ? s : encodeURIComponent(s);
        }

        function decode(s) {
                return config.raw ? s : decodeURIComponent(s);
        }

        function stringifyCookieValue(value) {
                return encode(config.json ? JSON.stringify(value) : String(value));
        }

        function parseCookieValue(s) {
                if (s.indexOf('"') === 0) {
                        // This is a quoted cookie as according to RFC2068, unescape...
                        s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
                }

                try {
                        // Replace server-side written pluses with spaces.
                        // If we can't decode the cookie, ignore it, it's unusable.
                        // If we can't parse the cookie, ignore it, it's unusable.
                        s = decodeURIComponent(s.replace(pluses, ' '));
                        return config.json ? JSON.parse(s) : s;
                } catch(e) {}
        }

        function read(s, converter) {  //读方法
                var value = config.raw ? s : parseCookieValue(s); //是否需要解析cookie值
                return $.isFunction(converter) ? converter(value) : value;  //结果是否需要转换
        }

        var config = $.cookie = function (key, value, options) {

                // Write  存cookie值

                if (value !== undefined && !$.isFunction(value)) {
                        options = $.extend({}, config.defaults, options);

                        if (typeof options.expires === 'number') {   //如果为数字类型,则直接已天数相加
                                var days = options.expires, t = options.expires = new Date();
                                t.setTime(+t + days * 864e+5);
                        }

                        return (document.cookie = [
                                encode(key), '=', stringifyCookieValue(value),
                                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE 
                                options.path    ? '; path=' + options.path : '',
                                options.domain  ? '; domain=' + options.domain : '',
                                options.secure  ? '; secure' : ''
                        ].join(''));
                }

                // Read

                var result = key ? undefined : {};

                // To prevent the for loop in the first place assign an empty array
                // in case there are no cookies at all. Also prevents odd result when
                // calling $.cookie().
                var cookies = document.cookie ? document.cookie.split('; ') : [];

                for (var i = 0, l = cookies.length; i < l; i++) {
                        var parts = cookies[i].split('=');
                        var name = decode(parts.shift());
                        var cookie = parts.join('=');

                        if (key && key === name) {
                                // If second argument (value) is a function it's a converter...
                                result = read(cookie, value);
                                break;
                        }

                        // Prevent storing a cookie that we couldn't decode.
                        if (!key && (cookie = read(cookie)) !== undefined) {
                                result[name] = cookie;
                        }
                }

                return result;
        };

        config.defaults = {};

        $.removeCookie = function (key, options) {
                if ($.cookie(key) === undefined) {
                        return false;
                }

                // Must not alter options, thus extending a fresh object...
                //不得修改选项,否则会产生新的对象. 删除时的参数必须与设置时的options一样
                $.cookie(key, '', $.extend({}, options, { expires: -1 }));
                return !$.cookie(key);
        };

}));

 

分享到:
评论

相关推荐

    jquery.cookie.js下载

    jquery.cookie.js下载 jquery.cookie.js下载

    jquery.cookie.js,jquery.min.js

    2. **jQuery Cookie Plugin**: `jquery.cookie.js`是jQuery的一个插件,用于处理浏览器的Cookie。这个插件允许开发者方便地读取、设置和删除Cookie,这对于实现用户会话管理、个性化设置等功能非常有用。 3. **...

    jquery.cookie.js包

    `jquery.cookie.js`是jQuery的一个扩展插件,它使得在Web应用中管理和操作Cookie变得更加简单。本篇文章将深入探讨`jquery.cookie.js`包及其在实现“记住密码”功能中的应用。 首先,我们来了解什么是Cookie。...

    jquery.cookie.min.js

    最新的JQUERY.COOKIE.MIN.JS

    cookie设置插件jquery.cookie.min.js

    cookie设置插件jquery.cookie.min.js 文章《javascript设置cookie高级篇可跨域访问》https://blog.csdn.net/cplvfx/article/details/117822956

    jquery.cookie.js插件源码绿色工具

    在标题提到的“jquery.cookie.js插件源码绿色工具”中,我们关注的是如何使用该插件来保存用户的主题选择,以便他们在下次访问时仍能看到他们之前选择的主题。 ### 1. jQuery Cookie基本用法 首先,确保你的项目中...

    jquery1.4.2.js和jquery.cookie.js

    总结,jQuery 1.4.2作为一款经典版本,提供了丰富的DOM操作和事件处理能力,而jQuery Cookie插件则弥补了JavaScript原生对Cookie操作的不足,两者结合,为开发者构建交互性强、用户体验优良的Web应用提供了便利。...

    jquery1.4 jquery,jquery-1.4,jquery1.4,最新jquery.js,jquery-1.4.min.js

    **jQuery 1.4:JavaScript库的里程碑版本** jQuery是一个广泛使用的JavaScript库,它极大地简化了HTML文档遍历、事件处理、动画制作以及Ajax交互。jQuery 1.4是该库的一个重要版本,发布于2010年,带来了许多性能...

    jquery.cookie.js

    引入JS文件操作cookie更加便捷,可以直接 $.cookie获取和设置cookie对象

    jquery.jsoncookie.js

    jquery.jsoncookie.js

    jquery.js和jquery.cookie.js

    `jquery.cookie.js`是jQuery的一个扩展插件,用于处理浏览器Cookie。Cookie在Web应用中常用于存储用户状态或设置。 8. **使用jQuery Cookie** 要使用jQuery Cookie,首先需要在页面加载后引入`jquery.cookie.js`...

    官方jquery.cookie.js带demo

    jQuery cookie是个很好的cookie插件,大概的使用方法如下 example $.cookie(’name’, ‘value’); 设置cookie的值,把name变量的值设为value example $.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, ...

    jQuery插件Cookie操作jQuery.Cookie -源码.zip

    在下载的源码压缩包中,主要包含一个名为`jquery.cookie.js`的文件,这是jQuery.Cookie的核心代码。通过阅读源码,我们可以看到插件是如何实现上述功能的。 1. `$.cookie`函数:首先,插件会检查浏览器是否支持...

    jquery.media.js.zip

    《jQuery.media.js:深入解析与应用》 在Web开发领域,jQuery库以其简洁的API和强大的功能,成为了JavaScript开发者们的首选工具。今天我们要探讨的是一个基于jQuery的插件——jQuery.media.js,它为网页中的多媒体...

Global site tag (gtag.js) - Google Analytics