Solved

How to read cookies in jquery

Posted on 2013-01-21
3
249 Views
Last Modified: 2013-02-10
I need to read some cookies in a javascript objects using jquery. Any code sample is appreciated.
0
Comment
Question by:ysgdel
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38803855
you've all here : https://github.com/carhartl/jquery-cookie
- plugin
- example
0
 
LVL 7

Expert Comment

by:aplusexpert
ID: 38805069
You can read cookie this way

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

Open in new window


Or you can also use JQuery cookie plugin.

Thanks.
0
 
LVL 10

Accepted Solution

by:
Monica P earned 500 total points
ID: 38805172
GetCookies in a page using javascript

http://www.w3schools.com/js/js_cookies.asp

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i = 0;i < ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question