JS Set cookie, delete cookie (citation)

Source: Internet
Author: User
Tags set cookie setcookie

JS Set cookie, delete cookie (citation)

There are many ways to set up a cookie in JS.

The first type: (This is the Code of the website)

<script>//Set cookiesfunction Setcookie (CNAME, cvalue, exdays) {var d =NewDate (); D.settime (D.gettime () + (exdays*24*60*60*1000));var expires ="expires="+D.toutcstring (); Document.cookie = CNAME +"="+ Cvalue +";" +Expires;}//Get cookiesfunction GetCookie (CNAME) {var name = cname +"=";var CA = Document.cookie.split (‘;‘);Forvar i=0; i<ca.length; i++) {var C =Ca[i];while (C.charat (0) = =‘') c = c.substring (1);if (C.indexof (name)! =-1)ReturnC.substring (Name.length, c.length); }Return"";}//Clearing cookiesfunction ClearCookie (name) {Setcookie (name,"", -1); } function Checkcookie () {var user = GetCookie ("Username");if (User! =""welcome Again  "+ user);} else" Span style= "color: #800000;" >please Enter your Name:); if (User! = null) {Setcookie ( Username365             

The second type:

<script>//JS Operation Cookies Method!//Write cookiesfunction Setcookie (c_name, value, Expiredays) {var exdate=NewDate (); Exdate.setdate (exdate.getdate () +Expiredays); document.cookie=c_name+"="+ Escape (value) + ((expiredays==NULL)?"" :"; expires="+Exdate.togmtstring ()); }//Read cookiesfunction GetCookie (name) {var arr,reg=New RegExp ("(^| )"+name+"=([^;] *)(;|$)");if (arr=Document.cookie.match (REG))Return (arr[2]);ElseReturnNull;}//Delete Cookiesfunction Delcookie (name) {var exp =NewDate (); Exp.settime (Exp.gettime ()-1);var cval=GetCookie (name);if (cval!=Null) document.cookie= name + =" ;expires= "+exp.togmtstring ();} // Use example Setcookie (username ",  darren< Span style= "color: #800000;" > ', 30 "alert (GetCookie (  "username"              

A third example

"Content-type"Content="text/html; Charset=utf-8"/> "Javascript"Type="Text/javascript">function Addcookie (objname, ObjValue, objhours) {//Add a cookievar str = objname +"=" +Escape (ObjValue);if (Objhours >0) {//When 0 does not set the expiration time, the cookie disappears automatically when the browser shuts downvar date =NewDate ();var ms = Objhours *3600 *1000; Date.settime (Date.gettime () +MS); str + ="; expires=" +Date.togmtstring (); } Document.cookie =Str Alert"Add Cookie Success"); } function GetCookie (objname) {//Gets the value of the cookie for the specified namevar 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 of the specified name, you can set its expiration time to a past timevar date =NewDate (); Date.settime (Date.gettime ()-10000); Document.cookie = name +"=a; expires=" +Date.togmtstring (); } function Allcookie () {//Read all saved cookie stringsvar str =Document.cookie;if (str = ="") {str ="No cookies are saved"; } alert (str); } function $ (m, N) {ReturnDocument.forms[m].elements[n].value; } function add_ () {var cookie_name = $ ("MyForm","Cookie_name");var Cookie_value = $ ("MyForm","Cookie_value");var cookie_expirehours = $ ("MyForm","Cookie_expireshours"); Addcookie (Cookie_name, Cookie_value, cookie_expirehours); } function get_ () {var cookie_name = $ ("MyForm","Cookie_name");var cookie_value =GetCookie (Cookie_name); alert (Cookie_value); } function Del_ () {var cookie_name = $ ("MyForm","Cookie_name"); Delcookie (Cookie_name); Alert"Delete Succeeded"); } </script> "MyForm"> <div> <labelFor="Cookie_name">Name </label> <input type="Text"Name="Cookie_name"/> </div> <div> <labelFor="Cookie_value">Values </lable> <input type="Text"Name="Cookie_value"/> </div> <div> <labelFor="Cookie_expirehours">How many hours of expiration </lable> <input type="Text"Name="Cookie_expireshours"/> </div> <div> <input type="button"Value="Add this cookie"Onclick="Add_ ()"/><input type= "button" Value= " read all Cookie" onclick=  "allcookie () "/> <input type= "button  "Value="  read the name cookie< Span style= "color: #800000;" > "Onclick=" get_ ()  "button" value= "Delete this name cookie" Onclick= "Del_ ()"/> </div> </form> </body>      

JS Set cookie, delete cookie (citation)

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.