Organizational Research By

Surprising Reserch Topic

How to set/unset cookie with jQuery?


How to set a cookie named 'test' and value '1'?

And especially, how to unset?


asked May 20, 2015 in JAVASCRIPT by rahulgii
0 votes
22 views



Related Hot Questions

3 Answers

0 votes
See the plugin:

https://github.com/carhartl/jquery-cookie

You can then do:

$.cookie("test", 1);
To delete:

$.removeCookie("test");
Additionally, to set a timeout of a certain number of days (10 here) on the cookie:

$.cookie("test", 1, { expires : 10 });
If the expires option is omitted, then the cookie becomes a session cookie, and is deleted when the browser exits.

To cover all the options:

$.cookie("test", 1, {
   expires : 10,           //expires in 10 days

   path    : '/',          //The value of the path attribute of the cookie
                           //(default: path of page that created the cookie).

   domain  : 'jquery.com',  //The value of the domain attribute of the cookie
                           //(default: domain of page that created the cookie).

   secure  : true          //If set to true the secure attribute of the cookie
                           //will be set and the cookie transmission will
                           //require a secure protocol (defaults to false).
});
To read back the value of the cookie:

var cookieValue = $.cookie("test");
You may wish to specify the path parameter if the cookie was created on a different path to the current one:

var cookieValue = $.cookie("test", { path: '/foo' });
answered May 20, 2015 by rahulgii
0 votes
No need to use jQuery particularly to manipulate cookies.

From QuirksMode (including escaping characters)

function createCookie(name, value, days) {
    var expires;

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = encodeURIComponent(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 decodeURIComponent(c.substring(nameEQ.length, c.length));
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
Take a look at
answered May 20, 2015 by rahulgii
0 votes

You can set the cookies as like

setCookie('test','1');
You can get the cookies as like

getCookie('test');
Hope it will helps to someone :)

EDIT:

If you want to save path of cookie for homepage alone then do this like

function setCookie(key, value) {
                var expires = new Date();
                expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
                document.cookie = key + '=' + value +';path=/'+ ';expires=' + expires.toUTCString();
            }
answered May 20, 2015 by rahulgii

...