Solved

How to read cookies in jquery

Posted on 2013-01-21
3
229 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now