jquery.cookie.js 分析代码

/**
 * 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. 设置一个 cookie 的值。
 * @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
 * @desc Create a cookie with all available options. 设置一个 cookie 的所有值。
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie. 创建一个 cookie 。
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. 删除一个 cookie
 *
 * @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/[email protected]
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie. 取得一个 cookie 的值。
 *
 * @param String name The name of the cookie. cookie 名称
 * @return The value of the cookie. 变量值
 * @type String	字符串
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/[email protected]
 */
 // 名称,值,选项
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
	// 如果值不为空,应该是设置一个值
        options = options || {};	// 变量 options 没有指定 就给它赋值空对象 {} Object
        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') {	// 此变量只能存为数字,这里的单位是天,可以自己修改69行。改变单位。
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));	//如果 options.expires 为负数一乘再加上当前时间,就是比当前时间早一些的时间,此cookie就是已经过期的了。就会删除掉。
            } else {
                date = options.expires;	// 如果变量不是数字,就直接取
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE , 返回date对象的世界标准时间(UTC)的字符串表示
        }
        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
	//否则只有名称,值为空,应该是读取一个 cookie 的值
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {		//要读 cookie ,先判断,存在,并不为空
            var cookies = document.cookie.split(';');		//解析 cookie
            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;
    }
};

此文章,对这个插件进行简单的分析。有个BUG(73行处,判断不严格),不过功能要按作者的要求写的话,还是不错的。


你可能感兴趣的:(JavaScript)