博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
前端cookie
阅读量:4476 次
发布时间:2019-06-08

本文共 5211 字,大约阅读时间需要 17 分钟。

cookie 封装js

/*** Cookie plugin** Copyright (c) 2006 ziqiu.zhang * Dual licensed under the MIT and GPL licenses:* http://www.opensource.org/licenses/mit-license.php* http://www.gnu.org/licenses/gpl.html* 使用举例:    //注: 写入时,subName参数请传递空值或null    //写入Cookies-值为字符串,即不包含子键    $.cookie("singleKey", "", "singleKey-value", { expires: 1, path: "/", secure: false })    //读取Cookies-根据主键    alert("singleKey:" + $.cookie("singleKey"));    //写入Cookies-值为对象,则每个属性名为子键的名称,属性值为子键值    var subNameObj = { subName1: "aaa", subName2: "bbb", subName3: "ccc" };    $.cookie("multiKey", "", subNameObj, { expires: 1, path: "/", secure: false });    //读取Cookies-根据主键    alert("multiKey:" + $.cookie("multiKey"));    //读取Cookies-根据主键和子键    alert("multiKey,subName1:" + $.cookie("multiKey", "subName1"));**/jQuery.cookie = function (name, subName, 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) : ';path=/';        var domain = options.domain ? '; domain=' + (options.domain) : '';        var secure = options.secure ? '; secure' : '';        //If value is an object, each property will be a sub key;        if (typeof value == "object") {            var k = 0;            var tempResult = "";            for (var tempValue in value) {                if (k > 0) {                    tempResult += "&";                }                tempResult += tempValue + "=" + encodeURIComponent(value[tempValue]);                k++;            }            value = tempResult;        }        else {            value = encodeURIComponent(value);        }        document.cookie = [name, '=', 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));                    //Search sub key                    if (typeof subName != 'undefined' && subName != null && subName != "") {                        var subCookies = cookieValue.toString().split('&');                        for (var j = 0; j < subCookies.length; j++) {                            var subCookie = jQuery.trim(subCookies[j]);                            if (subCookie.substring(0, subName.length + 1) == (subName + '=')) {                                cookieValue = decodeURIComponent(subCookie.substring(subName.length + 1));                                break;                            }                        }                    }                    break;                }            }        }        return cookieValue;    }};

写入cookie

var cityCookieKey = "Citys";        $(function () {            if ($.cookie(cityCookieKey, "values") != null && $.cookie(cityCookieKey, "name") != null) {  //读取                $("#citys").attr("lang", $.cookie(cityCookieKey, "values"));                $("#citys").html($.cookie(cityCookieKey, "name") + " ");            }            $(".check_city").addClass("hide");            $(".check_city").hide();            $(".check_city a").bind("click", function () {                if ($.cookie(cityCookieKey, "values") != null && $.cookie(cityCookieKey, "name") != null) {                    $.cookie(cityCookieKey, "", "", { expires: 1, path: "/", secure: false }); //写入                }                $(".check_city").addClass("hide");                $(".check_city").hide();                $(".check_city").removeClass("show");                var citys = $(this).html();                var value = $(this).attr("lang");                var cookieFilter = {                    values: value,                    name: citys                };                $.cookie(cityCookieKey, "", cookieFilter, { expires: 1, path: "/", secure: false });  //写入                $("#citys").attr("lang", $.cookie(cityCookieKey, "values"));                $("#citys").html($.cookie(cityCookieKey, "name") + " ");            });        });

 

转载于:https://www.cnblogs.com/llxy/p/4171241.html

你可能感兴趣的文章
蒟蒻吃药计划-治疗系列 #round5 采药+数字组合代码存放
查看>>
Git
查看>>
ImageSwitcher 右向左滑动的实现方式
查看>>
数学之美读书笔记一信息的度量和作用
查看>>
《荣枯鉴》示伪卷八
查看>>
NLP 第10章 基于深度学习的NLP 算法
查看>>
win7下出现'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件的解决方法...
查看>>
Maven 依赖范围(转)
查看>>
Google Chrome中的高性能网络(转)
查看>>
[置顶] 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
查看>>
Tomcat在处理GET和POST请求时产生的乱码问题
查看>>
XSS 攻击原理及防护
查看>>
操作符重载
查看>>
Docker 安装及问题处理
查看>>
JavaScript中的call 和apply的用途以及区别
查看>>
HashMap完全解读
查看>>
匿名内部类
查看>>
man命令重定向后有^H乱码问题
查看>>
自定义popupwindow(解决位置控制困惑)
查看>>
BZOJ4071: [APIO2015]八邻旁之桥
查看>>