Cookies are browsers (user agents) access to a number of Web sites, these sites are stored in the client's set of data, used to make Web sites such as tracking users, implementation of user-defined features.
The domain and path properties of the cookie identify which Web site The cookie is sent to the browser, and the Expires property of the cookie identifies the valid time of the cookie, and the data is automatically deleted when the cookie's valid time has passed.
If you do not set an expiration time, the cookie life cycle is the duration of the browser session, and the cookie disappears when the browser window is closed. This lifetime is called a session cookie for browsing session cookies. Session cookies are generally not stored on the hard drive but are kept in memory. If an expiration time is set, the browser saves the cookie to the hard disk and then opens the browser again after it is closed, and the cookies remain valid until the set expiration time is exceeded. Cookies stored on your hard disk can be shared between different browser processes, such as two IE windows. For cookies stored in memory, different browsers have different ways of handling them.
Set cookies
The code is as follows |
Copy Code |
function Setcookie (name, value,days) { Three parameters, one is the name of the cookie, one is the value, the last is the number of days var exp = new Date (); New Date ("December 31, 9998"); Exp.settime (Exp.gettime () + days * 24 * 60 * 60 * 1000); Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring (); } |
Read cookies
The code is as follows |
Copy Code |
function GetCookie (name) { Take cookies function var arr = Document.cookie.match (New RegExp ("(^|)" + name + "= ([^;] *)(;|$)")); if (arr!= null) Return unescape (arr[2]); return null; }
|
Delete Cookies
The code is as follows |
Copy Code |
function Delcookie (name) { Delete cookies and show them again The function checks whether the cookie is set, and if it is set, sets the expiration time to the previous time; The rest is left to the operating system to clean up cookies at the right time. if (GetCookie (name)) { Document.cookie = name + "= +"; Expires=thu, 01-jan-70 00:00:01 GMT "; } } |
Use cookies to change skin
The code is as follows |
Copy Code |
var themes = { ' Gray ': ' Tpl/gray.css ', ' Black ': ' Tpl/black.css ', ' Bootstrap ': ' Tpl/bootstrap.css ', ' Default ': ' Tpl/default.css ', ' Metro ': ' Tpl/metro.css ' }; var skins = $ ('. Li-skinitem span '). Click (function () { var $this = $ (this); if ($this. Hasclass (' cs-skin-on ')) return; Skins.removeclass (' cs-skin-on '); $this. addclass (' cs-skin-on '); var skin = $this. attr (' rel '); $ (' #swicth-style '). attr (' href ', Themes[skin]); Setcookie (' Cs-skin ', skin); Skin = ' dark-hive '? $ ('. Cs-north-logo '). CSS (' color ', ' #FFFFFF '): $ ('. Cs-north-logo '). CSS (' color ', ' #000000 '); }); if (GetCookie (' Cs-skin ')) { var skin = GetCookie (' Cs-skin '); $ (' # Swicth-style '). attr (' href ', Themes[skin]); $this = $ ('. Li-skinitem span[rel= ' +skin+ '); $this. addclass (' cs-skin-on '); skin = = ' dark-hive ' $ ('. Cs-north-logo '). CSS (' color ', ' #FFFFFF '): $ ('. Cs-north-logo '). CSS (' Color ', ' #000000 '); } |