JavaScript manipulation Cookie Method Instance Analysis _javascript skill

Source: Internet
Author: User

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.

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.