A, cookie
What is a Cookie?
"Cookie is stored on the visitor's computer variables whenever the same computer requests a page with a browser, it will send the cookie you can use JavaScript to create and retrieve cookie values..." - w3school
A cookie is a file created by the site visited to store browsing information, such as personal profile information.
From JavaScript's point of view, cookie information is some string. This information is stored in the client computer, for transmitting information between the client computer and the server.
In JavaScript can be read or information provided by document.cookie. Since the cookie is used in between the client and server communicate, so in addition to the JavaScript, server-side language (such as PHP) may access the cookie.
Note: If the page is a local disk, chrome console is unable to read and write operations cookie with JavaScript, solution ... for a browser ^ _ ^.
1. Set the cookie
When using JavaScript to access cookie, you must use the cookie property of the Document object; how to introduce a line of code to create and modify a cookie:
If you want to store a plurality of name / value pairs, spaces by a semicolon (;) separated
Document. cookie = " userIdsss = 666; userNameee = Lucas; path = /; Domain =" www.google.cn; Expires = 30; Secure "; // Set multiple cookie
tip: cookie identifies all properties are green
Code above 'usernameee' represents a cookie name, 'lucas' represents a value corresponding to the name. Assume cookie name does not exist, it is to create a new cookie; If there is a change in the value corresponding to the name of the cookie. If you want to create a cookie times, this method can be used repeatedly.
Not contain a semicolon in the name of the input or when the cookie value (;), comma (,), the equal sign (=) and space (required escape () function is encoded), when acquiring the cookie by unescape () function to value is converted back.
The cookie attribute set to Secure (belonging to the https protocol ) , to ensure that only encrypted data transmission between the server and the cookie, the cookie stored in the local file is not encrypted. If you want a local cookie is also encrypted, they have their own encrypted data.
Function wording :
function the setCookie (c_name, value, expiredays) { var exdate = new new a Date (); // initialize time exdate.setDate (exdate.getDate () + expiredays); // set period
// path to be filled, because the default path of JS It is the current page, if you do not fill, this cookie is only effective on the current page! c_name + = document.cookie "=" + Escape (value) + "; path = /" + ((expiredays == null ) "": ";? the Expires =" + exdate.toUTCString ()); // if given exiredays assign the label off remove the cookie }
2. Get cookie
Easy writing:
document.write(document.cookie)
Function writing:
function getCookie(name){ var strcookie = document.cookie;//获取cookie字符串 var arrcookie = strcookie.split("; ");//分割 //遍历匹配 for ( var i = 0; i < arrcookie.length; i++) { var arr = arrcookie[i].split("="); if (arr[0] == name){ return unescape(arr[1]); } } return ""; }
3. To delete a cookie
To delete a cookie, its expiration time can be set to a past time or the current time, for example:
function clearCookie(name) { the setCookie (name, "", -1); // this function above Reference 1. Set Cookie }
Clear cookie There are two ways :
-
- By Browser Tools Clear cookie (there are third-party tools, the browser itself has this feature)
- By setting the cookie is valid to clear the cookie
- Note: delete the cookie may sometimes cause some pages not function properly
4. Check the cookie
function checkCookie() { var user = getCookie("username"); if (user != "") { alert("Welcome again " + user); } else { user = prompt("Please enter your name:", ""); if (user != "" && user != null) { setCookie("username", user, 365); } } }
5. Clear all under one domain cookie
function clearAllCookie() { var keys = document.cookie.match(/[^ =;]+(?=\=)/g); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() } }
Continued update .....