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