首页 > 代码库 > 前端cookie
前端cookie
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 =http://www.mamicode.com/== 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 =http://www.mamicode.com/= "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 = http://www.mamicode.com/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") + " <img src=http://www.mamicode.com/"/Content/User/icon/add_icon.gif\" />"); } $(".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 = http://www.mamicode.com/$(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") + " <img src=http://www.mamicode.com/"/Content/User/icon/add_icon.gif\" />"); }); });
前端cookie
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。