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
Release: 2016-05-16 16:24:08
Original
1346 people have browsed it

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.

Related labels:
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template