9

My set Cookie js function

function setCookie(name, value, expires, path){
    cookieStr = name + "=" + escape(value) + "; ";

    if(expires){
        expires = setExpiration(expires);
        cookieStr += "expires=" + expires + "; ";
    }
    if(path){
        cookieStr += "path=" + path + "; ";
    }
    document.cookie = cookieStr;
}

When I create a cookie,

 setCookie('MyCookie','cookieName',3,'/Members')

How to get cookie's path?

2
  • 1
    Don't know about him, but i need it because i have multiple cookies with the same name available to the document that i need to distinguish by path. Commented Jul 13, 2017 at 4:44
  • @YairNevet I've a similar requirement where I need to clear duplicate cookies. Duplicates are present because they had different path when set. Commented Nov 10, 2017 at 9:12

1 Answer 1

15

TL:DR; You cannot read through cookies based on path using javascript.

In JavaScript, you can only set or get cookies by using the internal object document.cookie. And the content of this object will be a string of key value pairs of non-httpOnly cookie names and values separated by a ;. And that is pretty much it.

There is no way you could get a trace of Path, Domain and other attributes of cookies as they are only read by browsers and not shown to JavaScript.

On the other hand, If you are using any form of AJAX, You could try to intercept and parse the request headers by xhr.getResponseHeader("Set-Cookie") and store the value in localStorage or sessionStorage as per your need. I still advise you that it is not a good idea. Some of the browsers might consider Set-Cookie header as one of the forbidden headers to be read by javascript. but I think that restriction is only for httpOnly cookies.

Sign up to request clarification or add additional context in comments.

3 Comments

Things may have changed, there now be browser.cookies.getAll() which does provide the path among other things in a digestible object... however, getting access to this API (within compatible browsers) does require setting up a manifest.webmanifest file. And the browsers that do support such fanciness do so in different ways, which then requires another dependency (shim)... localStorage and sessionStorage seem way easier.
Surely you meant document.cookie, not document.cookies
As an addition to @SOAndSo 's comment: There is now the draft for the CookieStore API which seems to allow access to the attributes. I'm not sure if it is identical in behaviour to the "cookies" API of the previous comment, but it looks very similar. The cookieStore is already available in Chrome. There accessing the path currently works as follows: cookieStore.get("name").then(cookie => console.log(cookie.path))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.