JS Wrapper cookie Operation JS get cookie JS Set cookie JS Delete cookie

Source: Internet
Author: User
Tags set cookie

Learn a little bit of programming every day PDF ebook, video tutorial free download:
Http://www.shitanlife.com/code


var cookieutil = {Set cookie set:function (name, value, expires, domain, path, secure) {var cookietext =""; Cookietext + = encodeURIComponent (name) +"=" + encodeuricomponent (value);if (expires instanceof Date) {Cookietext + ="; Expires= "+ expires.togmtstring (); }if (path) {Cookietext + ="; Path= "+ path; }if (domain) {cookietext + ="; domain= "+ domain; }if (secure) {Cookietext + ="; Secure "; } document.cookie = Cookietext; },Name=value; Expires=expiration_time; Path=domain_path; Domain=domain_name; SecureGet cookie Get:function (name) {var cookiename = encodeuricomponent (name) +"=", Cookiestart = Document.cookie.indexOf (cookiename), Cookievalue ="";if (Cookiestart >-1) {var cookieend = document.cookie.indexOf (";", Cookiestart);if (cookieend =-1) {cookieend = Document.cookie.length;} Cookievalue = decodeURIComponent (document.cookie.substring (Cookiestart + cookiename.length, cookieend)); } return cookievalue;}, //Delete cookie unset:function (name, Domain, path, secure) {this.set (name, 0), domain, path, secure); } }; //test Cookieutil.set ( "name", " Zhang "); var name = Cookieutil.get ( "name"); alert (name); //Zhang Cookieutil.unset (" name "); Alert (Cookieutil.get " name ")); //empty










We propose the concept of a sub-cookie for storing small amounts of cookie data. is to store multiple data in the value of each cookie, using the"&" separated.
    var subcookieutil = {/** set a full cookie * param name: Indicates the name of the cookie, required * param subcookies: The value of the cookie, as an object, required * para        M Expires: Indicates the expiration time of the cookie, you can not fill out * param domain: The domain name that represents the cookie, you can not fill in * param path: The path of the cookie, you can not fill        * Param secure: Indicates the security mark of the cookie, can not fill in * EG:SUBCOOKIEUTIL.SETALL ("info", {name: "Zhang", age:23}); **/setall:function (Name, subcookies, expires, domain, path, secure) {var cookietext ="", subname, cookieparts = []; Cookietext + = encodeURIComponent (name) +"=";For (SubName in subcookies) {Cookieparts.push (encodeURIComponent (subname) +"=" + encodeURIComponent (Subcookies[subname])); }if (Cookieparts.length >0) {Cookietext + = Cookieparts.join ("&");if (expires instanceof Date) {Cookietext + ="; Expires= "+ expires.togmtstring (); }if (path) {Cookietext + ="; Path= "+ path; }if (domain) {cookietext + ="; domain= "+ domain; }if (secure) {Cookietext + ="; Secure "; } }else {Cookietext + ="; Expires= "+ Date (0). togmtstring (); } document.cookie = Cookietext; },/** set a sub-cookie * param name: Indicates the name of the cookie, required * param subname: Indicates the name of the sub-cookie, required * param value: Indicates the value of the child cookie, required * param EXP Ires: Indicates the expiration time of the cookie, you can not fill out * param domain: The domain name that represents the cookie, you can not fill in * param path: The path of the cookie, you can not fill in * param secure: The security flag that represents the cookie , you can not fill out * Eg:SubCookieUtil.set ("info", "Sex", "boy"); **/set:function (name, SubName, value, expires, domain, path, secure) {var cookies =This.getall (name) | | {}; Cookies[subname] = value;This.setall (name, cookie, expires, domain, path, secure); },/** read a full cookie * param name: Indicates the name of the cookie, required * Return: A Cookie Object * EG:SUBCOOKIEUTIL.GETALL ("info"); **/getall:function (name) {var cookiename = encodeuricomponent (name) +"=", Cookiestart = Document.cookie.indexOf (cookiename), Cookievalue ="", I, Len, subcookies, parts, result = {};if (Cookiestart >-1) {var cookieend = document.cookie.indexOf (";", Cookiestart);if (Cookieend = =-1) {cookieend = document.cookie.length;} cookievalue = decodeURIComponent (document.cookie.substring (Cookiestart + Cookiename.length, Cookieend));if (Cookievalue.length >0) {subcookies = Cookievalue.split ("&");for (i =0, len = subcookies.length; i < Len; i++) {parts = Subcookies[i].split ("="); Result[decodeuricomponent (parts[0])] = decodeURIComponent (parts[1]); }return result; } }ReturnNull },/** gets the value of a note cookie * param name: Indicates the name of the cookie, required * param subname: The name of the child cookie * return: The value of a sub-cookie * eg:subcookieutil . Get ("info", "name"); **/get:function (name, subname) {var cookies =This.getall (name);if (cookies) {return Cookies[subname]; }else {ReturnNull } },/** Delete a full cookie * param name: Indicates the name of the cookie, required * param domain: The domain name that represents the cookie, can not fill in * param path: The path of the cookie, can not be filled * param s Ecure: Indicates the security mark of the cookie, can not fill in * EG:SUBCOOKIEUTIL.UNSETALL ("info"); **/unsetall:function (name, domain, path, secure) {This.setall (Name,"", Date (0). togmtstring (), domain, path, secure); },/** Delete a sub-cookie * param name: Indicates the name of the cookie, required * param subname: Indicates the name of the sub-cookie, required * param domain: The domain name that represents the cookie, can not be filled * param Path: Represents a cookie, can not be filled with * param secure: The security flag for the cookie, you can not fill in * Eg:SubCookieUtil.unset ("info", "name"); **/unset:function (name, subname, domain, path, secure) {var cookies =This.getall (name);if (cookies) {delete Cookies[subname];This.setall (name, cookies,NULL, domain, path, secure); } } };Test:var Zhang = {name:"Zhang", Age:At height:"178cm", Weight:"66kg"}Set up a full cookie subcookieutil.setall ("Zhang", Zhang);Get a full cookievar Zhang = subcookieutil.getall ("Zhang"); alert (zhang.weight);66kgAdd a sub-cookie for Zhang Subcookieutil.set ( "Sport", //gets the child cookie alert (Subcookieutil.get ( "Zhang", " sport ")); //basketball //Delete a sub-cookie Subcookieutil.unset ( "age"; Alert (Subcookieutil.get ( "Zhang",  "age")); //undefined //delete a full cookie Subcookieutil.unsetall ( "Zhang"); Alert (Subcookieutil.getall ( "Zhang")); //error because it has been deleted 




Learn a little about programming PDF ebook, video tutorial free download:
/http Www.shitanlife.com/code

JS Wrapper cookie Operation JS get cookie JS Set cookie JS Delete cookie

Related Article

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.