JQuery plug-in, jquery plug-in Library
Cookie is a small text file that the website designer places on the client. Cookies can provide users with a lot of services. For example, a shopping website stores the list of products that users have browsed, or a portal website remembers what kind of news users like to browse. Users can store user login information as permitted, so that users do not have to enter this information every time they visit the website.
Usage:
1. Introduce jquery. cookie. js
2. Write cookies to files
[Javascript]View plaincopyprint?
- VarCOOKIE_NAME = 'username ';
- If($. Cookie (COOKIE_NAME )){
- $ ("# Username"). val ($. cookie (COOKIE_NAME ));
- }
- $ ("# Check"). click (Function(){
- If(This. Checked ){
- $. Cookie (COOKIE_NAME, $ ("# username"). val (), {path: '/', expires: 10 });
- // Var date = new Date ();
- // Date. setTime (date. getTime () + (3*24*60*60*1000); // expire after three days
- // $. Cookie (COOKIE_NAME, $ ("# username"). val (), {path: '/', expires: date });
- }Else{
- $. Cookie (COOKIE_NAME,Null, {Path: '/'}); // Delete the cookie
- }
- });
Parameter settings:
Expires: (Number | Date) validity period. You can set an integer as the validity period (in days) or a Date object as the Cookie expiration Date. If the specified date is negative, the cookie will be deleted. If it is not set or set to null, the cookie will be processed as a Session Cookie and deleted after the browser is closed.
Path: (String) path attribute of the Cookie. By default, it is the page path for creating the cookie.
Domain: (String) the domain name attribute of the Cookie. By default, it is the domain name of the page that creates the cookie.
Secure: (Boolean) if it is set to true, the transmission of this cookie requires a security protocol, such as HTTPS
Usage
Create session cookie:
$.cookie('the_cookie', 'the_value');
Create expiring cookie, 7 days from then:
$.cookie('the_cookie', 'the_value', { expires: 7 });
Create expiring cookie, valid authentication SS entire site:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
Read cookie:
$.cookie('the_cookie'); // => "the_value" $.cookie('not_existing'); // => undefined
Read all available cookies:
$.cookie(); // => { "the_cookie": "the_value", "...remaining": "cookies" }
Delete cookie:
// Returns true when cookie was found, false when no cookie was found...$.removeCookie('the_cookie');
// Same path as when the cookie was written...$.removeCookie('the_cookie', { path: '/' });
Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie, unless you're relying on the default options that is.
Configurationraw
By default the cookie value is encoded/decoded when writing/reading, usingencodeURIComponent
/decodeURIComponent
. Bypass this by setting raw to true:
$.cookie.raw = true;
Json
Turn on automatic storage of JSON objects passed as the cookie value. AssumesJSON.stringify
AndJSON.parse
:
$.cookie.json = true;
Cookie Options
Cookie attributes can be set globally by setting properties of$.cookie.defaults
Object or individually for each call$.cookie()
By passing a plain object to the options argument. Per-call options override the default options.
Expires
expires: 365
Define lifetime of the cookie. Value can beNumber
Which will be interpreted as days from time of creation orDate
Object. If omitted, the cookie becomes a session cookie.
Path
path: '/'
Define the path where the cookie is valid.By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior ).If you want to make it available for instance authentication ss the entire domain usepath: '/'
. Default: path of page where the cookie was created.
Note regarding Internet Explorer:
Due to an obscure bug in the underlying WinINET InternetGetCookie implementation, IE's document. cookie will not return a cookie if it was set with a path attribute containing a filename.
(From Internet Explorer Cookie Internals (FAQ ))
This means one cannot set a path usingpath: window.location.pathname
In case such pathname contains a filename like so:/check.html
(Or at least, such cookie cannot be read correctly ).
Domain
domain: 'example.com'
Define the domain where the cookie is valid. Default: domain of page where the cookie was created.
Secure
secure: true
If true, the cookie transmission requires a secure protocol (https). Default:false
.
Converters
Provide a conversion function as optional last argument for reading, in order to change the cookie's value to a different representation on the fly.
Example for parsing a value into a number:
$.cookie('foo', '42'); $.cookie('foo', Number); // => 42
Dealing with cookies that have been encoded usingescape
(3rd party cookies ):
$.cookie.raw = true; $.cookie('foo', );
You can pass an arbitrary conversion function.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.