JS adds a Cookie to get the cookie value to delete the cookie, jscookie

Source: Internet
Author: User

JS adds a Cookie to get the cookie value to delete the cookie, jscookie

Function setCookie (name, value) {var Days = 30; var exp = new Date (); exp. setTime (exp. getTime () + Days * 24x60*60*1000); document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString (); var strsec = getsec (time); var exp = new Date (); exp. setTime (exp. getTime () + strsec * 1); document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();} // read cookiesfunction getCookie (name) {Var arr, reg = new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"); if (arr = document. cookie. match (reg) return (unescape (arr [2]); else return null;} // Delete cookiesfunction delCookie (name) {var exp = new Date (); exp. setTime (exp. getTime ()-1); var cval = getCookie (name); if (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();}


How to Use js to dynamically create, delete, and set values in cookies,

Read the manual by yourself

Baidu w3cschool

How does JS delete a cookie?

Js reads the cookie, js adds the cookie, and js deletes the cookie. The following is an example:
<! Doctype html public "-// W3C // dtd html 4.01 // EN" "www.w3.org/TR/html4/strict.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB2312"/>
<Title> cookie processing function exercise (I wrote it, not what I think: Improving object-oriented) </title>
<Script language = "JavaScript" type = "text/javascript">
Function addCookie (objName, objValue, objHours) {// Add cookie
Var str = objName + "=" + escape (objValue );
If (objHours> 0) {// when the value is 0, no expiration time is set. When the browser is disabled, the cookie disappears automatically.
Var date = new Date ();
Var MS = objHours x 3600*1000;
Date. setTime (date. getTime () + MS );
Str + = "; expires =" + date. toGMTString ();
}
Document. cookie = str;
Alert ("cookie added successfully ");
}

Function getCookie (objName) {// obtain the cookie value of the specified name
Var arrStr = document. cookie. split (";");
For (var I = 0; I <arrStr. length; I ++ ){
Var temp = arrStr [I]. split ("= ");
If (temp [0] = objName) return unescape (temp [1]);
}
}

Function delCookie (name) {// to delete a cookie with the specified name, you can set its expiration time to a previous time.
Var date = new Date ();
Date. setTime (date. getTime ()-10000 );
Document. cookie = name + "= a; expires =" + date. toGMTString ();
}

Function allCookie () {// read all saved cooki... the remaining full text>

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.