Using jquery to manipulate cookies implementation code _jquery

Source: Internet
Author: User
When you visit a website, you will produce a very small text file on your hard disk, which can record your user ID, password, browsed pages, time of stay, and so on.

When you come to the site again, the site by reading cookies, learn about your information, you can make the appropriate action, such as on the page to show you welcome the slogan, or let you do not enter the ID, password directly login and so on. In essence, it can be seen as your identity card.

The use of traditional JavaScript to set up and get cookies information is cumbersome, to write a few functions to deal with, fortunately, jquery to help us do a lot of things, with the help of jquery plug-ins, we can easily create, acquire and delete cookies. Click here to download the Cookies plugin: Http://plugins.jquery.com/project/Cookie

Creating cookies

Using jquery to set cookies is easy. For example, we create a cookie named "Example" with the value "foo":

Copy Code code as follows:

$.cookie ("Example", "foo");

To set the expiration date for a cookie, you can set the expires value, such as setting the cookie expiration of 10 days:
Copy Code code as follows:

$.cookie ("Example", "foo", {expires:10});

Set cookies to expire after one hour:
Copy Code code as follows:

var cookietime = new Date ();
Cookietime.settime (Date.gettime () + (* * 1000));//coockie Save one hour
$.cookie ("Example", "foo", {expires:cookietime});

To set the save path for a cookie, you can set the path value, such as setting the root directory:
Copy Code code as follows:

$.cookie ("Example", "foo", {path: "/"});

If you want to set the path to/admin, then:
Copy Code code as follows:

$.cookie ("Example", "foo", {path: "/admin"});

Get Cookie Value
A way to get the value of a cookie using jquery is fairly straightforward, and the following is a pop-up box that displays the value of a cookie named "Example":
Copy Code code as follows:

Alert ($.cookie ("example"));

Delete Cookies
To remove a cookie using jquery, you only need to null the value of the cookie, and note that if you set a string with an empty value, you cannot delete the cookie, just empty the cookie value.
Copy Code code as follows:

$.cookie ("example", null);

After understanding the cookie-related settings operation method, and then in the next article, I will use examples to illustrate the application of cookies, such as recording the history of users browsing the site (viewed products, movies), stay tuned.

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.