I know that many people actually need this function when developing a website, which is to control users to keep clicking on the top! So I will briefly write about the operation of COOkie based on javascript!
The code copy is as follows:
//Set cookies
function setCookie(key, value) {
document.cookie = key + "=" + escape(value);
}
//Get the value of the 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 let’s give you a simple example! that is
The code copy is as follows:
//According to the id sent in by clicking
function comment(id,is){
if(getCookie(id)==null){
setCookie(id,"www.widuu.com");
alert("Set cookies successfully");
}else{
if(getCookie(id)=="www.widuu.com"){
alert("You have commented");
return ;
}
//Here is the value saved to the database through ajax
}
Although this function is very simple, it is very practical. If you need it, please change it! Take a look at the screenshot!
Has it implemented the functions that friends often need? It's very simple. If you need it, just take it away and use it.