Jquery. cookie function method that is compatible with IE to obtain and set cookies _ jquery-js tutorial

Source: Internet
Author: User
When using IE for testing, we found the common in Discuz. the getcookie and setcookie methods in js do not work under IE, so jquery is available. cookie. the origin of js. If you are interested, refer Preface

In the development process, because we have been in touch with Discuz before, we can directly use its common. the getcookie and setcookie methods in js are used for subsequent tests. When I find that these two methods do not work in IE, I will ask my colleagues, in this way, jquery is available. cookie. the origin of js files contains very few codes. I will paste them below to facilitate future use and research.

Source code

The Code is as follows:


/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* Http://www.opensource.org/licenses/mit-license.php
* Http://www.gnu.org/licenses/gpl.html
*
*/

/**
* Create a cookie with the given name and value and other optional parameters.
*
* @ Example $. cookie (the _ cookie, the _ value ');
* @ Desc Set the value of a cookie.
* @ Example $. cookie (the _ cookie, the _ value, {expires: 7, path: '/', domain: 'jquery. com ', secure: true });
* @ Desc Create a cookie with all available options.
* @ Example $. cookie (the _ cookie, the _ value ');
* @ Desc Create a session cookie.
* @ Example $. cookie (the _ cookie, null );
* @ Desc Delete a cookie by passing null as value.
*
* @ Param String name The name of the cookie.
* @ Param String value The value of the cookie.
* @ Param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @ Option Number | Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* When the browser exits.
* @ Option String path The value of the path atribute of the cookie (default: path of page that created the cookie ).
* @ Option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie ).
* @ Option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* Require a secure protocol (like HTTPS ).
* @ Type undefined
*
* @ Name $. cookie
* @ Cat Plugins/Cookie
* @ Author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given name.
*
* @ Example $. cookie (the _ cookie ');
* @ Desc Get the value of a cookie.
*
* @ Param String name The name of the cookie.
* @ Return The value of the cookie.
* @ Type String
*
* @ Name $. cookie
* @ Cat Plugins/Cookie
* @ Author Klaus Hartl/klaus.hartl@stilbuero.de
*/
JQuery. 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. toUTCString )){
Var date;
If (typeof options. expires = 'number '){
Date = new Date ();
Date. setTime (date. getTime () + (options. expires * 24x60*60*1000 ));
} Else {
Date = options. expires;
}
Expires = '; expires =' + date. toUTCString (); // 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.