A simple way to set cookies in JS

Source: Internet
Author: User
Tags set cookie setcookie

One, JS set the use of cookies

PHP can set cookies, using JS to set cookies What is the benefit? In the front of the operation, sometimes to pass many parameters, this time, we can splice these parameters to the URL after the transfer value, there is receiving parameters, but this is very troublesome, if the data into the cookie, the developer's development efficiency, I think will be higher, to Taobao for example, He has a classification, level two classification, three, four, we look for goods, may involve a lot of conditions, if these conditions are put into the cookie, I feel more convenient, do not know Taobao is not like this. Taobao's URL is added secret, and very long, it may be the root in the back of the URL. Crap not to say a little more to see a small example.

Two, JS instance


Get cookies
function GetCookie (name) {
var Nameeq = name + "=";
var ca = Document.cookie.split (';'); Split cookies into groups
for (Var i=0;i < ca.length;i++) {
var c = Ca[i]; Get string
while (C.charat (0) = = ") {//Determine if the string has a leading space
c = c.substring (1,c.length); If you have one, take it from the second place.
}
if (C.indexof (nameeq) = = 0) {//If the name we want is included
Return unescape (c.substring (nameeq.length,c.length)); Decode and intercept the value we want.
}
}
return false;
}

Clear cookies
function ClearCookie (name) {
Setcookie (Name, "",-1);
}

Set cookies
function Setcookie (name, value, seconds) {
seconds = seconds | |   0; Seconds has a value on the direct assignment, not 0, this root PHP is not the same.
var expires = "";
if (seconds!= 0) {//Set cookie Lifetime
var date = new Date ();
Date.settime (Date.gettime () + (seconds*1000));
expires = "; Expires= "+date.togmtstring ();
}
Document.cookie = name+ "=" +escape (value) +expires+ ";   path=/"; Transfer code and assign value
}

Call the above method:



Setcookie ("test", "Tank", 1800);        //Set cookie value, live for half an hour  
Alert (GetCookie (' Test '));                     //Get the value of the cookie, display tank 
ClearCookie ("test");                            //Delete the value of the cookie  
Alert (GetCookie (' Test '));                     // Test the corresponding cookie value is null and is displayed as false. Is the value of the GetCookie last return.  

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.