Jquery.cookie usage and its points of attention

Source: Internet
Author: User
Tags sublime text

Jquery.cookie is a lightweight cookie plugin that can be used as it has been packaged.

Basic Create, read, delete see another article talk about Localstorage, sessionstorage and cookies.

From the name you can see that Jquery.cookie is dependent on jquery, so when using Jquery.cookie, you should first introduce a jquery file, and then introduce the Jquery.cookie file.

Create Cookie: $.cookie (' name ', ' Zhang San ')

Then use $.cookie (' name ') on the trip.

But what if you want to deposit multiple data at once? What if you want to put an object {' name ': ' Zhang San ', ' age ': ' Three ', ' sex ': ' Man '} into a cookie named person?

Like this, okay? $.cookie (' person ', {' name ': ' Zhang San ', ' age ': ' A ', ' sex ': ' Man '}) . Although this creates success, the result can be obtained through $.cookie ('person') :

[Object Object]. However, when you use $.cookie (' person '). Name to get it, the result is undefined. Visible, this is not feasible.

The reason is thata cookie is essentially a txt text, so it can only be stored in a string, the object is usually serialized before it can be stored in a cookie, and the object is reversed before it can be obtained

So, at the time of deposit, it can be written like this, $.cookie (' person ', json.stringify ({' name ': ' Zhang San ', ' Age ': ' "'", ' sex ': ' Man ' ) '), and then get the time to deserialize it, Pass

Json.parse ($.cookie (' person ')). Name to get success.

The pit daddy has the wood.

Again to say the attention matters :

  (1), when using Jquery.cookie is required to use the server (can use Tomcat, Appserve or sublime text, etc.), If you only use Jquery.cookie in a local static file, and then open it directly through the browser, you will find that the result is undefined, this problem makes me suspect life.

  (2), when the cookie is not valid, the cookie is created by default until the user closes the browser , and therefore also referred to as a session cookie

Jquery.cookie usage and its points of attention

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.