A class _javascript technique for modifying cookies in JavaScript

Source: Internet
Author: User
Tags set cookie
It is mainly through the analysis of the Document.cookie string to perform functional assembly.
brush up on the cookie action in javascript:
Adding cookies can be achieved by document.cookie= "userid=111";
The full version can be used:Document.cookie= "userid=111;domain=.google.com;path=\;secure=secure;expire=" +date.togmtstring ();
You can set the expiration time of the cookie, the domain name, the path
Needs to be removed as long as the expire time is set to now.
Now on the javascript.cookie.js I modified the class
Copy Code code as follows:

/*
Cookie Helper Class
Easy to Write,get,delete
*/
var mycookie={
Get:function (name) {
if (typeof name!= "undefined")
{
If name given call the Get Value function
return Mycookie_get (name);
}else{
If name isn't given,i want get all cookie item
return Mycookie_getall ();
}
},
Add:function (name,value,options) {
Write the cookie
Mycookie_add (name,value,options);
},
Delete:function (name) {
Delete the cookie
Mycookie_add (Name,null);
}
}
String.prototype.Trim = function ()
{
Return This.replace (/^\s+/g, ""). Replace (/\s+$/g, "");
}
/*
Cookie Write function
@name: The cookie name is not NULL
@value: The cookie value null==delete the cookie
@option: {"Expires": Expire time; " Path ":/;" Domain ": localhost;" Secure ": Secure}
*/
function Mycookie_add (name,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 isn't supported by IE
}
var path = Options.path? '; Path= ' + options.path: ';
var domain = Options.domain? '; Domain= ' + options.domain: ';
var secure = options.secure? '; Secure ': ';
Document.cookie = [name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join (');
}
}
/*
Get the name Cookie
@name: The cookie ' s name
*/
function Mycookie_get (name)
{
var cookievalue = null;
if (document.cookie && document.cookie!= ") {
var cookies = Document.cookie.split (';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i]. Trim ();
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));
Break
}
}
}
return cookievalue;
}
/*
Get all the cookies return as a JSON
*/
function Mycookie_getall ()
{
var cookiearray = new Array ();
var str= "";
var temp;
if (document.cookie && document.cookie!= ") {
var cookies = Document.cookie.split (';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i]. Trim ();
Temp=cookie.split (' = ');
Take the
Cookiearray.push ("{\" name\ ": \" "+decodeuricomponent (temp[0)) +" \ "" value\ ": \" "+decodeuricomponent (Temp[1]) +" \ "} ");
}
Str=cookiearray.join (",");
}
Str= "[" +str+ "]";
Return eval (' (' +str+ ') ');
}

The call is also fairly simple
Copy Code code as follows:

Mycookie.add ("UserAccount", "admin", {"Expires": 5})//Add a cookie for a period of 5 days
Alert (Mycookie.get ("UserAccount"));//Remove Cookie
Cookies=mycookie.get ()//Get all the cookies
for (Var i=0;i<cookies.length;i++)
{
Alert (cookies[i]["name"]+ ":" +cookies[i]["value"]);
}
Mycookie.delete ("UserAccount");//delete the cookie that you just added
Alert (Mycookie.get ("UserAccount"));

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.