One, the JQuery.Cookie.js plugin is a lightweight Cookie management plugin.
Special reminder, today found a special error, Google browser tip: Has no method $.cookie. Firefox tip: $.cookie is not a function; debugging for half a day, and finally find the reason, if the same page two or more times to introduce the jquery plugin will report this error.
How to use:
1, the introduction of jquery and JQuery.Cookie.js plug-ins.
<script src= "Jquery.1.8.3.js" type= "Text/javascript" ></script> <script src= "Jquery.cookie.js" Type= "Text/javascript" ></script>
2, function.
Syntax: $.cookie (name, value, [option])
(1) Read Cookie value
$.cookie (cookiename) CookieName: The name of the cookie to read.
Example: $.cookie ("username"); Reads the value of the username stored in the cookie named.
(2) Write set cookie value:
$.cookie (Cookiename,cookievalue); CookieName: The cookie name to be set, Cookievalue represents the corresponding value.
Example: $.cookie ("username", "admin"); Write the value "admin" to a cookie named Username cookie.
$.cookie ("username", NULL); Destroying a cookie named username
(3) [option] parameter description:
Expires: A finite date, which can be an integer or a date (in days). This place should also note that if you do not set this thing, the cookie will expire after the browser is closed.
Path:cookie the path to the saved value, which is the same as creating the page path by default.
The Domin:cookie domain Name property, by default, is the same as creating a page domain name. This place should pay considerable attention to the concept of cross-domain, if you want the primary domain two domain name valid, you want to set the ". xxx.com"
Secrue: A Boolean value that indicates whether a security protocol is required when transferring cookie values.
Example:
$.cookie ("Like", $ (": radio[checked]"). Val (), { path: "/", Expiress:7 })
A full set of page codes to read the cookie:
A cookie is essentially a txt literal, 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 deserialized before it can be obtained.
$(function() {if ($.cookie ("o") = =Null) {var o = {name: "Zhang San", age:24 }; var str = json.stringify (o); // pairs serialized into a string and then deposited into the cookie $.cookie ("O", str, {expires:7 // set time, if left blank, the browser closes this cookie expires.) }); Alert ("Cookie is empty");} else { var str1 = $.cookie ("o"); var O1 = Json.parse (STR1); // characters are deserialized into Object alert (o1.name); /// output The name value of the deserialized Object }})
jquery Plugin Cookie