File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Loop through cookies and get value for a particular cookie name. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Loop through cookies and get value for a particular cookie name." Watch "Loop through cookies and get value for a particular cookie name." New topic
Author

Loop through cookies and get value for a particular cookie name.

forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Hello fellow Ranchers,

Can anybody point me to code/resources on how to loop through cookies to get hold of the value for a particular cookie.

Thanks

Srinivas
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I would start here: JavaScript getCookie

Eric
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Eric Pascarello wrote:I would start here: JavaScript getCookie

Eric


Hi Eric,

I followed your google search and found this resource



The above code seems flawed, since it won't search my cookie that I have created earlier. As a result, it is freshly creating another cookie with the same name.

Something strange is happening, the new cookie is getting created at a different path than the earlier one!

When I check details about document.cookie , after I have already created one with a name 'selection', I don't see selection anywhere in the cookie (because it is searching from a different path!)

Is it possible to loop through 2 paths, and get all the cookies. Here are the 2 paths




Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
That code is not creating a different cookie. It is taking the cookie and splitting it up and storing it in an object.

How are you creating the cookie? When the cookie is created are you setting the domain?

Eric
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Eric Pascarello wrote:
How are you creating the cookie? When the cookie is created are you setting the domain?
Eric


Here is my setCookie code:



The above 'alert' won't print the cookie that I created earlier with the same name (as it was already created at a different path)
Is this because I am not considering the path ? The domain is same for the 2 cookies, only the path is different...

Thanks

Srinivas
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
How is the cookie being created?

Eric
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
When i create cookie, its getting saved at a certain location. I click a button and go to a different tab ( a different page). I still see the cookie.

When I click back to original screen, the browser is not able to retrieve the cookie that I created earlier. And my code will create a new cookie if one doesn't exist already. This new cookie is getting created at a different location. Now I see the same cookie name at 2 different paths.

How to fix this?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop through cookies and get value for a particular cookie name.