This example describes the JavaScript manipulation cookie method. Share to everyone for your reference. as follows:
My methods for setting, reading and deleting cookies. I have methods to check for the existence of cookies names or values,//To retrieve by name or value, and to create a
formatted string of//All cookies. My site:andrew.dx.am var setcookie = function (name, value, expires, path, domain, secure) {//the caller should Tr
Im the name/value pair, if required. Sets the Name/value pair (encoded); ' Expires ' is the No.
Days.
var expires_date;
if (expires) {expires_date = new date ();
Expires_date.setdate (Expires_date.getdate () + expires); } document.cookie = encodeURIComponent (name) + "=" + encodeuricomponent (value) + ((expires)? "; expires=" + expires_date.toutcstring (): "") + ((path)?; Path= "+ Path:" "+ (domain)?"; domain= "+ domain:" ") + ((secure)?";
Secure ":" ");
var Deletecookie = function (name, path, domain) {//The caller should Trim the name/value pair. Encodes the name before deLeting. Document.cookie = encodeURIComponent (name) + "=" + ((path)? ";p ath=" + Path: "") + ((domain)?;
domain= "+ Domain:" "") + "Expires=fri, 01-jan-2010 00:00:01 UTC";
var delallcookies = function () {var currdate = new Date (), I, Thecookie = Document.cookie.split (";");
Currdate = Currdate.toutcstring ();
i = thecookie.length; while (i--) {document.cookie = Thecookie[i] + ";
expires = "+ currdate;
}
};
var escapereg = function (str) {//Helper fn:escapes characters for use in a regular expression.
Return Str.replace (/[-[\]{} () *+?., \\^$|#\s]/g, "\\$&");
};
The following four functions does not Trim the name or value//-The calling FNS should does this.
var cnameexists = function (cookie_name) {//case-insensitive var testname, Myreg;
if (Document.cookie.length = = 0) return false;
TestName = Escapereg (cookie_name); Myreg = new RegExp (' (^|;)? ' + testname + ' = ([^;]
*) (; |$) ', ' I '); Return Myreg.test (decodeURIComponent (docUment.cookie));
};
var cvalueexists = function (cookie_value) {//Case insensitive var testname, Myreg;
if (Document.cookie.length = = 0) return false;
TestName = Escapereg (Cookie_value);
Myreg = new RegExp (' (=) ' + testname + ' (; |$) ', ' I ');
Return Myreg.test (decodeURIComponent (Document.cookie));
};
var cnameget = function (cookie_value) {//case-insensitive Var testname, myreg, results;
if (Document.cookie.length = = 0) return ";
TestName = Escapereg (Cookie_value); Myreg = new RegExp (' (^|;)?
[^=]*) = ' + TestName + ' (; |$) ', ' I ');
Results = decodeURIComponent (document.cookie). Match (Myreg); return (results)?
RESULTS[2]: ';
};
var cvalueget = function (cookie_name) {//case-insensitive Var testname, myreg, results;
if (Document.cookie.length = = 0) return ";
TestName = Escapereg (cookie_name); Myreg = new RegExp (' (^|;)? ' + testname + ' = ([^;]
*) (; |$) ', ' I ');
Results = decodeURIComponent (document.cookie). Match (Myreg); return (results)? RESULTS[2]: '';
}; var cookiestr = function () {//Returns a string (with line breaks) which could to//placed in, for example, a text
Area.
Return decodeURIComponent (Document.cookie). Replace (/([^=;] +)=([^;] *) [\s]*/g, ' $ \ n '] | |
'';
};
The
wants this article to help you with your JavaScript programming.