zoukankan      html  css  js  c++  java
  • JavaScript-Tool:jquery.cookie.js

    ylbtech-JavaScript-Tool:jquery.cookie.js
    1.返回顶部
    1、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) {
        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.
                s = decodeURIComponent(s.replace(pluses, ' '));
            } catch(e) {
                return;
            }
    
            try {
                // If we can't parse the cookie, ignore it, it's unusable.
                return config.json ? JSON.parse(s) : s;
            } catch(e) {}
        }
    
        function read(s, converter) {
            var value = config.raw ? s : parseCookieValue(s);
            return $.isFunction(converter) ? converter(value) : value;
        }
    
        var config = $.cookie = function (key, value, options) {
    
            // Write
            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.setDate(t.getDate() + days);
                }
    
                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) {
                // Must not alter options, thus extending a fresh object...
                $.cookie(key, '', $.extend({}, options, { expires: -1 }));
                return true;
            }
            return false;
        };
    
    }));
    2、
    2. 操作返回顶部
    1、

    http://w3school.com.cn/js/js_cookies.asp

    jquery.cookie中的操作:

    jquery.cookie.js是一个基于jquery的插件,点击下载

    创建一个会话cookie:

    $.cookie(‘cookieName’,'cookieValue’);

    注:当没有指明cookie时间时,所创建的cookie有效期默认到用户浏览器关闭止,故被称为会话cookie。

    创建一个持久cookie:

    $.cookie(‘cookieName’,'cookieValue’,{expires:7});

    注:当指明时间时,故称为持久cookie,并且有效时间为天。

    创建一个持久并带有效路径的cookie:

    $.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/'});

    注:如果不设置有效路径,在默认情况下,只能在cookie设置当前页面读取该cookie,cookie的路径用于设置能够读取cookie的顶级目录。

    创建一个持久并带有效路径和域名的cookie:

    $.cookie(‘cookieName’,'cookieValue’,{expires:7,path:’/',domain: ‘chuhoo.com’,secure: false,raw:false});

    注:domain:创建cookie所在网页所拥有的域名;secure:默认是false,如果为true,cookie的传输协议需为https;raw:默认为false,读取和写入时候自动进行编码和解码(使用encodeURIComponent编码,使用decodeURIComponent解码),关闭这个功能,请设置为true。

    获取cookie:

    $.cookie(‘cookieName’);   //如果存在则返回cookieValue,否则返回null。

    删除cookie:

    $.cookie(‘cookieName’,null);

    注:如果想删除一个带有效路径的cookie,如下:$.cookie(‘cookieName’,null,{path:’/'});

    2、
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    编译原理 First集和Follow集的求法
    编译原理——算符优先分析法详解
    api.js(接口文件)
    addmul.wxs(保留两位小数-将手机号中间4位数变成*-处理时间戳)
    插槽的使用
    scroll-view小程序侧边栏(点击加载右侧商品)
    Array.of
    es6解构赋值默认值结合使用
    ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面。
    es6 数组的新方法 some filter indexOf 展开运算符... let const
  • 原文地址:https://www.cnblogs.com/storebook/p/9121037.html
Copyright © 2011-2022 走看看