JavaScript manipulation Cookie Method Instance analysis

Source: Internet
Author: User
Tags regular expression trim

This example describes the JavaScript manipulation cookie method. Share to everyone for your reference. Specifically as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 F ormatted string of//All cookies. My site:andrew.dx.am var setcookie = function (name, value, expires, path, domain, secure) {//the caller should Trim 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 = Enco Deuricomponent (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 Befo Re 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 = Currdat E.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) retur n 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 textarea. 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.