I know a lot of people in the development of the site when the need for this function, is to control the user has been the top of anything! So I'm here to write a simple, JavaScript-based cookie operation!
Copy Code code as follows:
Set cookies
function Setcookie (key, value) {
Document.cookie = key + "=" + escape (value);
}
Get the value of a cookie
function GetCookie (key) {
if (document.cookie.length) {
var cookies = ' + Document.cookie;
var start = Cookies.indexof (' + key + ' = ');
if (start = = 1) {return null;}
var end = Cookies.indexof (";", start);
if (end = = 1) {end = Cookies.length;}
End-= start;
var cookie = cookies.substr (start,end);
Return unescape (Cookie.substr (cookie.indexof (' = ') + 1, cookie.length-cookie.indexof (' = ') + 1));
}
else {return null;}
}
Then give us a simple example of it! It is
Copy Code code as follows:
According to the ID that was passed in by click
function Comment (id,is) {
if (GetCookie (ID) ==null) {
Setcookie (ID, "www.widuu.com");
Alert ("Set cookie Success");
}else{
if (GetCookie (id) = = "Www.widuu.com") {
Alert ("You have already said");
return;
}
Here's your own logic. Values saved to the database via Ajax
}
Although this function is very simple but very practical, if there is a need to take to change it! Cut a picture, everybody!
Do you realize the functions that small partners often need? Very simple, there are need for small partners to take the use of it directly.