WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

How can I use jQuery to read a cookie and store it in a variable?

I'd like to read the contents of the "bar" cookie and store it in variable "foo"

My first stab was this, but I'm not getting an alert. I think the assignment statement just isnt correct.

var foo = $.cookie('bar');
alert (foo);

Upvotes: 0

Views: 7628

Answers (4)

Reza Nabati
Reza Nabati

Reputation: 162

first of all, you must download jQuery cookie plugin in here. subsequently add this link to the head tag of your document.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>

For Example Your Html Document has this tags:

<a href="#" id="showCookie">Show cookie</a>
<a href="#" id="deleteCookie">Delete cookie</a>
<input disabled id="verbose" type="text" />

for reading cookie add this script to your html page:

$("#showCookie").click(function(e){
  e.preventDefault();
var yourcookie=$.cookie(cookieName).val();
  $("#verbose").val("Cookie value is " + yourcookie + ".");

});

so and for creating cookie:

jQuery(document).ready(function(){

var cookieName = 'level';
var cookieOptions = {expires: 7, path: '/'};
$.cookie(cookieName, $(this).attr("id"), cookieOptions);
)};

and end of all deleting cookie:

$("#deleteCookie").click(function(e){
e.preventDefault();
  $("#verbose").val("Cookie 'level' with value \"" + $.cookie(cookieName) + "\" removed.");
  $("#" + $.cookie(cookieName)).removeClass("selected");

Upvotes: 2

NDBoost
NDBoost

Reputation: 10634

The use of a library is required if you wish to have jQuery handle the cookie manipulation.

jQuery Cookie Plugin By carhartl

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

Some people frown on using a library when you can do it so easily in straight-up javascript.. Personally, i've always used the library above.

However, If you wish to explore the non-jQuery way...

Straight-up JavaScript Method

Google search provided this page: http://www.tutorialspoint.com/javascript/javascript_cookies.htm

I've never used straight-up js to manipulate cookies, so i'll just copy/paste from that site for OPs ease.. You can reference the link just above this paragraph for specifics..

Write A Cookie

document.cookie = "key1=value1;key2=value2;expires=date";
function WriteCookie()
{
   var cookievalue= "ABCD";
   document.cookie="name=" + cookievalue;
   alert("Setting Cookies : " + "name=" + cookievalue );
}

Read All Cookies

function ReadCookie()
{
   var allcookies = document.cookie;
   alert("All Cookies : " + allcookies );

   // Get all the cookies pairs in an array
   cookiearray  = allcookies.split(';');

   // Now take key value pair out of this array
   for(var i=0; i<cookiearray.length; i++){
      name = cookiearray[i].split('=')[0];
      value = cookiearray[i].split('=')[1];
      alert("Key is : " + name + " and Value is : " + value);
   }
}

Upvotes: 2

Jeff Watkins
Jeff Watkins

Reputation: 6357

Do you have a/the jQuery cookie plugin? Here's one for you. Cookies aren't part of jQuery Core (yet).

Upvotes: 1

Oscar
Oscar

Reputation: 13980

I think you need this:

http://archive.plugins.jquery.com/project/Cookie

Upvotes: 1

Related Questions