GithubHelp home page GithubHelp logo

yzpnet / cookies Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jaaulde/cookies

0.0 0.0 0.0 252 KB

Javascript library for accessing and manipulating HTTP cookies in the web browser.

License: MIT License

JavaScript 100.00%

cookies's Introduction

cookies

Javascript library for accessing and manipulating HTTP cookies in the web browser.

Get one or a list of cookies, set cookies, delete cookies, test if the browser accepts cookies. When JSON support is available, any JS value can be set to a cookie--it will be automatically serialized before being written, and un-serialzied on read.

jQuery Plugin: The jQuery plugin which was once distributed directly with this library has been moved to its own project (which depends on this one).

GitHub version Bower version NPM version

installation

bower install jaaulde-cookies
npm install jaaulde-cookies

html

Download the code, link it in your HTML file.

<script src="/path/to/jaaulde-cookies.js"></script>

usage

This library is intended for use in the browser to access and manipulate cookies. It provides a singleton API, cookies.

Cookie options

As you'll see in the docs below, many of the methods can take an options parameter. The options that can be set are:

Option Description Default Note
domain Domain for which the cookie be available null (current domain)
path Path for which the cookie be available '/'
expires Date object representing expiration date/time of cookie null (expires when browser closes) Setting a past date/time will delete the cookie
secure Should cookie be sent to server via HTTPS only? false

Test for browser cookie acceptance

cookies.test()

signature
/**
 * test - test whether the browser is accepting cookies
 *
 * @access public
 * @static
 * @return {boolean}
 */
test: function ()
example
if (cookies.test()) {
    // browser is accepting cookies!
}

Set cookies

cookies.set()

signature
/**
 * set - set or delete a cookie with desired options
 *
 * @access public
 * @static
 * @param {string} n - name of cookie to set
 * @param {mixed} v - Any JS value. If not a string and JSON support present will be JSON encoded
 *                  {null} to delete
 * @param {object} o - optional list of cookie options to specify
 * @return {void}
 */
set: function (n, v, o)
examples
// sets cookie by the name of 'myCookie' to value of 'myValue' with default options
cookies.set('myCookie', 'myValue');

// sets cookie by the name of 'myCookie' to value of 'myValue' with path of '/somedir'
cookies.set('myCookie', 'myValue', {path: '/somedir'});

Get cookies

cookies.get()

signature
/**
 * get - get one, several, or all cookies
 *
 * @access public
 * @static
 * @param {mixed} n {string} name of single cookie
 *                  {array} list of multiple cookie names
 *                  {void} if you want all cookies
 * @return {mixed} type/value of cookie as set
 *                 {null} if only one cookie is requested and is not found
 *                 {object} hash of multiple or all cookies (if multiple or all requested)
 */
get: function (n)
examples
// returns value of myCookie if it is present, null if not
var my_cookie = cookies.get('myCookie');

// returns object in key/value form of each requested cookie if it is present, null if not
var some_cookies = cookies.get(['myCookie', 'myOtherCookie']);

// returns object in key/value form of all available cookies from your site
var all_cookies = cookies.get();

Get filtered list of Cookies

cookies.filter()

signature
/**
 * filter - get hash of cookies whose names match the provided RegExp
 *
 * @access public
 * @static
 * @param {RegExp} p The regular expression pattern to match against cookie names
 * @return {object} hash of cookies whose names match the RegExp
 */
filter: function (p)
examples
// returns object in key/value form of cookies whose names start with "site"
var filtered_cookies = cookies.filter(/^site/);

Delete Cookies

note: A cookie can only be deleted using the same options with which it was set

cookies.del()

signature
/**
 * del - delete a cookie (domain and path options must match those with which the cookie was set; this is really an alias for set() with parameters simplified for this use)
 *
 * @access public
 * @static
 * @param {mixed} n {string} name of cookie to delete
 *                  {boolean} true to delete all
 * @param {object} o optional list of cookie options to specify (path, domain)
 * @return {void}
 */
del: function (n, o)
examples
// deletes a cookie, 'myCookie', with default options
cookies.del('myCookie');

// deletes a cookie by the name of 'myCookie' which had been set with a path of '/somedir'
cookies.del('myCookie', {path: '/somedir'});

// deletes all cookies
cookies.del(true);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.