JQuery plug-in, jquery plug-in Library

Source: Internet
Author: User

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?
  1. VarCOOKIE_NAME = 'username ';
  2. If($. Cookie (COOKIE_NAME )){
  3. $ ("# Username"). val ($. cookie (COOKIE_NAME ));
  4. }
  5. $ ("# Check"). click (Function(){
  6. If(This. Checked ){
  7. $. Cookie (COOKIE_NAME, $ ("# username"). val (), {path: '/', expires: 10 });
  8. // Var date = new Date ();
  9. // Date. setTime (date. getTime () + (3*24*60*60*1000); // expire after three days
  10. // $. Cookie (COOKIE_NAME, $ ("# username"). val (), {path: '/', expires: date });
  11. }Else{
  12. $. Cookie (COOKIE_NAME,Null, {Path: '/'}); // Delete the cookie
  13. }
  14. });
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.stringifyAndJSON.parse:

$.cookie.json = true;
Cookie Options

Cookie attributes can be set globally by setting properties of$.cookie.defaultsObject 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 beNumberWhich will be interpreted as days from time of creation orDateObject. 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.pathnameIn 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.

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.