217

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

Community
  • 1
  • 1
Agus Puryanto
  • 1,355
  • 4
  • 16
  • 15

4 Answers4

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.
Shadoath
  • 720
  • 1
  • 15
  • 31
Ramesh Soni
  • 15,867
  • 28
  • 93
  • 113
  • @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 – jackerman09 Sep 27 '13 at 02: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? – Ramesh Soni Sep 27 '13 at 14:19
  • @RameshSoni, I was specifically referring to the https://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! – jackerman09 Sep 27 '13 at 18:24
  • 8
    there is a newer version here https://github.com/js-cookie/js-cookie, the API is a little different – Sam Watkins Aug 11 '15 at 03:05
  • not working in laravel as well – StealthTrails Dec 09 '15 at 19:12
  • Native javascript provides a very simple way to access Cookies without jQuery, hope this help https://www.w3schools.com/js/js_cookies.asp e.g. `document.cookie` – Samuel Chan Feb 10 '18 at 13:43
136

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
Shurvir Mori
  • 2,288
  • 1
  • 17
  • 29
balexandre
  • 73,608
  • 45
  • 233
  • 342
11

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

Damien
  • 1,140
  • 15
  • 22
louis_coetzee
  • 393
  • 7
  • 14
6

Google is my friend and it showed me this page:

Community
  • 1
  • 1
Natrium
  • 30,772
  • 17
  • 59
  • 73
  • 12
    downvoted for being a 'smartiepants' answer. – Josh Brown May 14 '18 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." https://stackoverflow.com/help/how-to-answer – Luke Stevenson Sep 28 '18 at 04:59