Front-End Cookies

Source: Internet
Author: User
Tags set cookie

Cookie Encapsulation 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* Use Example://Note: When writing, subname parameter pass NULL value or NULL//write cookies-value to string, that is, do not include sub-key $.cookie ("Singlekey", "", "Singlekey-value", {    expires:1, Path: "/", secure:false})//Read cookies-According to the primary key alert ("Singlekey:" + $.cookie ("Singlekey"));    Write cookies-value to object, each property name is a subkey name, the property value is a subkey value var subnameobj = {subName1: "AAA", SubName2: "BBB", SubName3: "CCC"};    $.cookie ("Multikey", "", Subnameobj, {expires:1, Path: "/", secure:false});    Read cookies-According to the primary key alert ("Multikey:" + $.cookie ("Multikey")); Read cookies-based on primary key and subkey alert ("MULTIKEY,SUBNAME1:" + $.cookie ("Multikey", "subName1"));*/Jquery.cookie=function (name, subname, value, options) {if(typeofValue! ='undefined') {//name and value given, set cookieoptions = Options | | {}; if(Value = = =NULL) {Value="'; Options.expires= -1; }        varexpires ="'; if(Options.expires && (typeofOptions.expires = =' Number'||options.expires.toUTCString)) {vardate; if(typeofOptions.expires = =' Number') {Date=NewDate (); Date.settime (Date.gettime ()+ (Options.expires * -* -* -* +)); } Else{Date=Options.expires; } Expires='; expires='+ date.toutcstring ();//Use expires attribute, Max-age isn't 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 ...        varPath = Options.path?'; path='+ (Options.path):';p ath=/'; varDomain = Options.domain?'; domain='+ (Options.domain):"'; varSecure = options.secure?'; secure':"'; //If value is an object and each property would be a sub key;        if(typeofValue = ="Object") {            varK =0; varTempresult ="";  for(varTempvalueinchvalue) {                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        varCookievalue =NULL; if(Document.cookie && Document.cookie! ="') {            varcookies = Document.cookie.split (';');  for(vari =0; i < cookies.length; i++) {                varCookie =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(typeofSubName! ='undefined'&& SubName! =NULL&& SubName! ="") {                        varSubcookies = Cookievalue.tostring (). Split ('&');  for(varj =0; J < Subcookies.length; J + +) {                            varSubcookie =Jquery.trim (Subcookies[j]); if(Subcookie.substring (0, Subname.length +1) = = (SubName +'=') ) {Cookievalue= decodeURIComponent (subcookie.substring (Subname.length +1));  Break; }                        }                    }                     Break; }            }        }        returnCookievalue; }};

Write Cookie

  varCitycookiekey ="Citys"; $ (function () {if($.cookie (Citycookiekey,"Values") !=NULL&& $.cookie (Citycookiekey,"name") !=NULL) {//read $ ("#citys"). attr ("Lang", $.cookie (Citycookiekey,"Values")); $("#citys"). HTML ($.cookie (Citycookiekey,"name") +"&nbsp;"); }            $(". 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 }); Write} $ (". Check_city"). AddClass ("Hide"); $(". Check_city"). Hide (); $(". Check_city"). Removeclass ("Show"); varCitys = $ ( This). html (); varValue = $ ( This). attr ("Lang"); varCookiefilter ={values:value, name:citys}; $.cookie (Citycookiekey,"", Cookiefilter, {expires:1, Path:"/", Secure:false }); Write $ ("#citys"). attr ("Lang", $.cookie (Citycookiekey,"Values")); $("#citys"). HTML ($.cookie (Citycookiekey,"name") +"&nbsp;");        }); });

Front-End Cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.