Set, read, and delete cookie functions in Javascript

Source: Internet
Author: User
Tags set cookie

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 ');
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.