Js cookie: we only need to delete the cookie when the expires expiration time is available. Set the cookie to operate on the document. cookie. Let's take a look at several related functions.
Cookies are a set of data stored on the client after the browser (User Agent) accesses some websites. These websites are used for tracking users such as websites to implement User-defined functions.
The Domain and Path attributes of the Cookie identify the website that sent the Cookie to the browser. The Expires attribute of the Cookie identifies the Cookie validity period. When the Cookie validity period Expires, the data is automatically deleted.
If no expiration time is set, it indicates that the life cycle of the Cookie is the browser session. If the browser window is closed, the Cookie disappears. This Cookie is called a session Cookie. Session cookies are generally stored in memory instead of hard disks. If the expiration time is set, the browser will save the Cookie to the hard disk, and then open the browser again. These cookies are still valid until the preset expiration time is exceeded. Cookies stored on hard disks can be shared among different browser processes, such as two IE Windows. For Cookies stored in the memory, different browsers have different processing methods.
Set cookie
The Code is as follows: |
Copy code |
Function setCookie (name, value, days ){ // Three parameters: cookie name, value, and number of days Var exp = new Date (); // new Date ("December 31,999 8 "); Exp. setTime (exp. getTime () + days x 24x60*60*1000 ); Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString (); } |
Read cookie
The Code is as follows: |
Copy code |
Function getCookie (name ){ // Cookies Function Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )")); If (arr! = Null) Return unescape (arr [2]); Return null; }
|
Delete cookie
The Code is as follows: |
Copy code |
Function delCookie (name ){ // Delete the cookie and try again // Check whether the cookie is set. If so, adjust the expiration time to the past time; // Leave the cookie to the operating system for proper time. If (getCookie (name )) { Document. cookie = name + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT "; } } |
Use cookies for skin replacement
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 '? Certificate ('.cs-north-logo'0000.css ('color', '# ffff'): Certificate ('.cs-north-logo'0000.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 '? Certificate ('.cs-north-logo'0000.css ('color', '# ffff'): Certificate ('.cs-north-logo'0000.css ('color',' #000000 '); } |