Example, I have this cookie:
$.cookie("foo", "500", { path: '/', expires: 365 });
How do I get the value of that cookie and put it into a variable?
For example (I know this is not correct):
var foo = $.cookie("foo").val();
                Function explained: Loop through the ca array (i = 0; i < ca.length; i++), and read out each value c = ca[i]). If the cookie is found (c.indexOf(name) == 0), return the value of the cookie (c.substring(name.length, c.length). If the cookie is not found, return "".
In JavaScript, we can create, read, update and delete a cookie by using document. cookie property.
Domain: Specifies the domain name of the website. Name=Value: Cookies are stored in the form of name-value pairs. Path: Specifies the webpage or directory that sets the cookie. Secure: Specifies whether the cookie can be retrieved by any server (secure or non-secure).
It's just var foo = $.cookie("foo").
There's no need for a .val() call as you're not accessing the value of a DOM element.
function getCookieValue(cname) { // cname is nothing but the cookie value which 
                                 //contains the value
                    var name = cname + "=";
                      var decodedCookie = decodeURIComponent(document.cookie);
                      var ca = decodedCookie.split(';');
                      for(var i = 0; i <ca.length; i++) {
                        var c = ca[i];
                        while (c.charAt(0) == ' ') {
                          c = c.substring(1);
                        }
                        if (c.indexOf(name) == 0) {
                          return c.substring(name.length, c.length);
                        }
                      }
                      return "";
                    }
                        To get the value of a cookie, you can just call it's reference. For example:
$.cookie("foo", "somevalue");
alert($.cookie("foo"));
Will alert:
somevalue
                        By this way we can access
console.log($.cookie()); //It will gives the all cookies in the form of object
alert($.cookie('foo'));//it will give cookie foo value ie 500
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With