JS Settings Cookie

Source: Internet
Author: User
Tags setcookie

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

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

<script>//Set CookiesfunctionSetcookie (CNAME, cvalue, exdays) {varD =NewDate (); D.settime (D.gettime ()+ (exdays*24*60*60*1000)); varexpires = "expires=" +d.toutcstring (); Document.cookie= cname + "=" + Cvalue + ";" +expires;}//Get CookiesfunctionGetCookie (CNAME) {varName = CNAME + "="; varCA = Document.cookie.split ('; '));  for(vari=0; i<ca.length; i++) {        varc =Ca[i];  while(C.charat (0) = = ") c = c.substring (1); if(C.indexof (name)! =-1)returnc.substring (Name.length, c.length); }    return"";}//Clearing CookiesfunctionClearCookie (name) {Setcookie (name,"",-1); }  functionCheckcookie () {varuser = GetCookie ("username"); if(User! = "") {alert ("Welcome Again" +user); } Else{User= Prompt ("Please enter your name:", "" "); if(User! = "" && User! =NULL) {Setcookie ("username", user, 365); }}}checkcookie (); </script>

The second type:

<script>//JS operation cookies Method!//Write CookiesfunctionSetcookie (c_name, value, expiredays) {varExdate=NewDate (); Exdate.setdate (Exdate.getdate ()+expiredays); Document.cookie=c_name+ "=" + Escape (value) + ((expiredays==NULL) ? "": "; expires=" +exdate.togmtstring ()); } //Read CookiesfunctionGetCookie (name) {vararr,reg=NewRegExp ("(^|)" +name+ "= ([^;] *)(;|$)"); if(arr=Document.cookie.match (reg))return(arr[2]); Else        return NULL;}//Delete CookiesfunctionDelcookie (name) {varExp =NewDate (); Exp.settime (Exp.gettime ()-1); varCval=GetCookie (name); if(cval!=NULL) Document.cookie= name + "=" +cval+ "; expires=" +exp.togmtstring ();}//using the exampleSetcookie (' username ', ' Darren ', 30) alert (GetCookie ("Username"));</script>

A third example:

functionAddcookie (objname, ObjValue, objhours) {//Add a cookie                varstr = objname + "=" +Escape (ObjValue); if(Objhours > 0) {//when 0 does not set the expiration time, the cookie disappears automatically when the browser shuts down                    varDate =NewDate (); varms = Objhours * 3600 * 1000; Date.settime (Date.gettime ()+ms); STR+= "; Expires= "+date.togmtstring (); } document.cookie=str; Alert ("Add Cookie Succeeded"); }                         functionGetCookie (objname) {//gets the value of the cookie for the specified name                varArrstr = Document.cookie.split (";");  for(vari = 0; i < arrstr.length; i++) {                     vartemp = arrstr[i].split ("="); if(Temp[0] = =objname)returnUnescape (temp[1]); }             }                         functionDelcookie (name) {//to delete a cookie of the specified name, you can set its expiration time to a past time                varDate =NewDate (); Date.settime (Date.gettime ()-10000); Document.cookie= name + "=A; Expires= "+date.togmtstring (); }                         functionAllcookie () {//read all saved cookie strings                varstr =Document.cookie; if(str = = "") {str= "No Cookies Saved";             } alert (str); }                         function$ (m, n) {returnDocument.forms[m].elements[n].value; }                         functionadd_ () {varCookie_name = $ ("MyForm", "Cookie_name"); varCookie_value = $ ("MyForm", "Cookie_value"); varCookie_expirehours = $ ("MyForm", "cookie_expireshours");             Addcookie (Cookie_name, Cookie_value, cookie_expirehours); }                         functionget_ () {varCookie_name = $ ("MyForm", "Cookie_name"); varCookie_value =GetCookie (cookie_name);             alert (Cookie_value); }                         functionDel_ () {varCookie_name = $ ("MyForm", "Cookie_name");                 Delcookie (Cookie_name); Alert ("Delete Succeeded"); }         </script>  for= "Cookie_name" >name</label> <input type= "text" name= "Cookie_name"/> </div> <div > <label for= "Cookie_value" >value</lable> <input type= "text" name= "Cookie_value"/> </div> <di V> <label for= "Cookie_expirehours" >How many hours are out of date</lable> <input type= "text" name= "cookie_expireshours"/> </div> <div> <input type= "button" value= "Add this cookie" onclick= "add_ ()"/><input type= "button" value= "Read All Cookies" onclick= "Allcookie ()"/><input type= "button" value= "read the name cookie" onclick= "get_ ()"/><input Type= "button" value= "Delete the name cookie" onclick= "del_ ()"/> </div> </form> </body> </ Html>

Attention:

The Chrome browser does not get cookies locally. Must be available on the server. If it is local, you can put it under the WWW directory.

Google Chrome only supports the reading and writing of cookies on online websites, which is forbidden for local HTML cookie operations. So the following code if you write in a local HTML file, will pop up the dialog box content is empty.

Document.cookie = "Test=cooo";
alert (Document.cookie);

If this page is the content of the online website, the cookie content test=cooo and so on will be displayed normally.

JS Settings Cookie

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.