JS file cookie Access value sample code _javascript tips

Source: Internet
Author: User
Tags set cookie
Copy Code code as follows:

/*
Cookie Tool
How to use:
Stored value
var value = "7 Days";
Tools.cookie ("Day", value, {expires:7}); To save a string: "7 days" to a cookie for 5 days with the key "Day"
Take value
var v = tools.cookie ("Day"); Use the "Day" key to remove the value from the cookie
*/
Tools.cookie = function (name, value, options) {
if (typeof value!= ' undefined ') {//name and value given, set cookie
options = Options | | {};
if (value = = null) {
Value = ';
Options.expires =-1;
}
var expires = ';
if (options.expires
&& (typeof options.expires = = ' Number ' | | | options.expires.toGMTString)) {
var date;
if (typeof options.expires = = ' number ') {
Date = new Date ();
Date.settime (Date.gettime ()
+ (Options.expires * 24 * 60 * 60 * 1000));
} else {
date = Options.expires;
}
expires = '; Expires= ' + date.togmtstring (); Use expires
Attribute
Max-age is not
Supported by IE
}
var path = Options.path? '; Path= ' + options.path: ';
var domain = Options.domain? '; Domain= ' + options.domain: ';
var secure = options.secure? '; Secure ': ';
Document.cookie = [name, ' = ', encodeURIComponent (value), expires,
Path, domain, secure].join (');
else {//only name given, get cookie
var cookievalue = null;
if (document.cookie && document.cookie!= ") {
var cookies = Document.cookie.split (';');
for (var i = 0; i < cookies.length; i++) {
var cookie = Jquery.trim (Cookies[i]);
Does this cookie string begin with the name we want?
if (cookie.substring (0, name.length + 1) = = (name + ' = ')) {
Cookievalue = decodeuricomponent (cookie
. substring (name.length + 1));
Break
}
}
}
return cookievalue;
}
};

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.