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.