Introduction, encapsulation, addition, acquisition, deletion, and html5cookie in html5
Cookie is the data stored on the user's local terminal.
We use cookies to record passwords and time limits when logging on to a website, such as 7 days and 5 days,
This is the session cycle of the cookie, but the cookie is insecure at the same time. We can view the password entered by the user on the webpage and rely on the server environment,
When writing a cookie again, we need to set the storage path, and the obtained value is a string, and it is very small, only 4 kb,
Next we will encapsulate coookie
Set cookie encapsulation:
Function setCookie (name, value, iDay) {// name of the uploaded value, value, expiration time
If (iDay) {// if there is an expiration time, this condition is executed.
Var oDate = new Date (); // obtain the current event stamp
ODate. setDate (oDate. getDate () + iDay); // set the expiration event
Document. cookie = name + "=" + value + "; path =/; expires =" + oDate; // set cookie
} Else {// If there is an expiration time, execute this condition to set the cookie
Document. cookie = name + "=" + value + "; path =/"; // name, value, and root directory
}
}
Get cookie value
Function getCookie (name ){
Var arr1 = document. cookie. split (";"); get the cookie value and use ";" to cut it into an array.
For (var I = 0; I <arr1.length; I ++) {// loop this array
Var arr2 = arr1 [I]. split ("="); // you can use the split () method to split a two-dimensional array.
If (arr2 [0] = name) {// returns a two-dimensional array of loops. When the first value is the value you pass, the second value of this array is returned.
Return arr2 [1];
}
}
Return "";
}
Delete cookie
Function removeCookie (name ){
SetCookie ("jack", "1234",-1); // call setCookie again. The key is to set the expiration time to a negative value.
}
The above is my understanding of cookies.