JavaScript Cookies
/* Set Cookie*/function setcookie (c_name,c_value,expires) { var date = New date (); date.settime (Date.gettime () + (expires*60*1000)); document.cookie = c_name + "=" + c_value + "; expires= " + date.togmtstring ();} /* The parameters in this function have cookie name, value, and number of days to expire. First convert the number of days to a valid date, and then save the cookie name, value, and its expiration date in document.cookie object */function getcookie (c_name) { var name = c_name + "="; var cookie = document.cookie.split (';'); for (var i=0; i<cookie.length; i++) { var c = cookie[i].trim (); The //.trim () function is used to remove whitespace characters at both ends of a string. var c = cookiE[i].replace (/(^\s*) | ( \s*$)/g, "")//use a regular replacement of whitespace characters at both ends of the string. if (C.indexof (name) ==0) return c.substring (name.length,c.length); } return "";} //Save Cookie //setcookie (cookie name, cookie value, expiration time) setcookie ("Userman", "123", 5) // Here is a 5 minute (Date.gettime () + (expires*60*1000)) //get Cookie var userman = getcookie (" Userman "); $ (Class). val (Userman)
You cannot use semicolons (;), commas (,), Equals (=), spaces, and Chinese (errors or garbled characters) in the name or value of a cookie. If the above special characters must be used, the escape () function is required to encode (transcode in 16 binary numbers), and if you want to read the value of a cookie with a special character and Chinese, you need to decode the value by using unescape () after extracting it to get the original cookie value;
Use of cookies ()
The library file containing jquery is first included , followed by jquery. Cookies. JS library file.
<script type= "Text/javascript" src= "jquery.min.js" ></script> <script type= "Text/javascript" src= " Jquery.cookie.js "></script> or <script type=" Text/javascript "src=" Zepto.js "></script> < Script type= "Text/javascript" src= "Zepto.cookie.js" ></script>
Use of Jquery.cookie.js
var time = name of "username"//cookie var date = new Date (); Gets the current time Date.settime (Date.gettime () + 300000); System Current time + 5 minutes $ ("cookie"). Val ($.fn.cookie (time));//assigns the cookie value of name = Username to Id=cookie's label var detailtime = $ ("input"). the text ();//Gets the value of input var admin = $ ("#takerTime"). Val (detailtime)//Gets the input $.cookie assigned to Id=takertime (Time,admin,{path : "/", expires:date})
Use of Zepto.cookie.js
var time = name of "username"//cookie var date = new Date (); Get system Time Date.settime (Date.gettime () + 300000); System Current time + 5 minutes $ ("cookie"). Val ($.fn.cookie (time));//assigns the cookie value of name = Username to Id=cookie's label var detailtime = $ ("input"). the text ();//Gets the value of input var admin = $ ("#takerTime"). Val (detailtime)//Gets the input $.fn.cookie assigned to Id=takertime (time,admin,{ Path: "/", expires:date})
Parameter description
Syntax: $.cookie (cookie name, cookie value, {path, cookie valid time})
$.cookie (time); Reads the value of the time stored in the cookie named.
$.cookie (time,admin); Writes the value of admin to a cookie named Time cookie.
{path: '/', expires:date} Date=〉cookie save time, date time is 5 minutes, set Hour with sethours () and GetHours method: Date.sethours (Date.gethours () + 1 ), or by multiplying in milliseconds
The Path:cookie value saves the path, by default only the Web page that sets the cookie can read the Cookie;path: "/": Represents the root directory.
Expires: A finite date, the value can be a number (from the time the cookie is created, in days) or a Date object, and if no time is set, the cookie is invalidated after the browser is closed.
Use of JavaScript cookies ()