217

Somebody help me. How to create, read and erase some cookies with jQuery ?

0

4 Answers 4

377

Use JavaScript Cookie plugin

Set a cookie

Cookies.set("example", "foo"); // Sample 1
Cookies.set("example", "foo", { expires: 7 }); // Sample 2
Cookies.set("example", "foo", { path: '/admin', expires: 7 }); // Sample 3

Get a cookie

alert( Cookies.get("example") );

Delete the cookie

Cookies.remove("example");
Cookies.remove('example', { path: '/admin' }) // Must specify path if used when setting.
6
  • @RameshSoni, I tried to use this plugin with rails, but it is not working for me. Any idea if there are issues with this and Rails 4? Thanks Sep 27, 2013 at 2:08
  • 1
    @jackerman09 well this is client side plugin which works with jQuery. Doesn't matter if you are using rails. Could you provide more details on your issue? Sep 27, 2013 at 14:19
  • @RameshSoni, I was specifically referring to the github.com/RyanScottLewis/jquery-cookie-rails, which I realize you aren't specifically alluding to above, but when I include it in my Gemfile and application.js (immediately after //= require jQuery, as the installation instructions say to do), I get an error whenever I load the app about missing files. If I remember correctly, the error was something like "Sprockets::FileNotFound" (or something close to that, I'm not at my dev computer right now). Thanks! Sep 27, 2013 at 18:24
  • 8
    there is a newer version here github.com/js-cookie/js-cookie, the API is a little different Aug 11, 2015 at 3:05
  • not working in laravel as well Dec 9, 2015 at 19:12
137

As I know, there is no direct support, but you can use plain-ol' javascript for that:

// Cookies
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";               

    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

You can access like below,

createCookie("test","test",1); // to create new cookie

readCookie("test"); // to retrive data from cookie

eraseCookie("test"); // will delete that cookie
7
  • 1
    i have been use this script, but doesn't work Oct 21, 2009 at 8:24
  • @Agus ... works fine here, I have it in production projects and they work like a charm! what error are you getting?
    – balexandre
    Oct 21, 2009 at 10:01
  • 6
    Good ol javascript is the base of jQuery! This is the same code in many plugins. +1
    – Piotr Kula
    Jan 31, 2013 at 14:17
  • 1
    Cross-browser correctness is a constant problem with web development. Are cookies handled uniformly enough to directly use document.cookie everywhere? Can you say which browsers this has been tested with?
    – doug65536
    Jul 20, 2013 at 15:43
  • Works OK on Opera 29, Firefox 38, Chrome 42. IE 11 not working.
    – vidriduch
    May 15, 2015 at 11:05
11

Use jquery cookie plugin, the link as working today: https://github.com/js-cookie/js-cookie

0
5

Google is my friend and it showed me this page:

2
  • 14
    downvoted for being a 'smartiepants' answer.
    – Josh Brown
    May 14, 2018 at 20:23
  • 3
    "Provide context for links - Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." stackoverflow.com/help/how-to-answer Sep 28, 2018 at 4:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.