JavaScript settings, read, delete cookie functions

Source: Internet
Author: User
Tags setcookie

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

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.