JavaScript-based cookie operation implementation can only point to the _javascript technique

Source: Internet
Author: User
Tags set cookie setcookie

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.