Home >Web Front-end >JS Tutorial >The operation implementation of COOkie based on javascript can only be clicked once_javascript skills

The operation implementation of COOkie based on javascript can only be clicked once_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:24:081332browse

I know that many people actually need this function when developing websites, which is to control users to keep clicking! So I will briefly write here about the operation of COOkie based on javascript!

Copy code The code is as follows:

//Set cookie
function setCookie(key, value) {
          document.cookie = key "=" escape(value);
}
//Get the value of 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 me give you a simple example! That’s it

Copy code The code is as follows:

//According to the id passed in according to the click
function comment(id,is){
If(getCookie(id)==null){
setCookie(id,"www.widuu.com");
alert("Cookie set successfully");
        }else{
If(getCookie(id)=="www.widuu.com"){
alert("You have already reviewed");
                 return ;
}
//Here is your own logic to save the value to the database through ajax
}

Although this function is very simple, it is very practical. If necessary, use it to modify it! Take a screenshot for everyone to see!


Have you implemented the functions that friends often need? It's very simple. If you need it, just take it and use it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn