How to get the domain value for a cookie in Javascript?

前端 未结 2 1117
无人及你
无人及你 2020-12-08 18:52

Using Javascript I\'d like to get the domain value for a specific cookie.

Is this possible? If so, how?

To clarify: I\'m not looking for the

相关标签:
2条回答
  • 2020-12-08 19:24

    Sorry, all you get is what you see in document.cookie. The cookie metadata like path, domain and expires are not visible to site code (neither to JavaScript nor to the server-side).

    To read a cookie that is being shadowed by a cookie with a more-specific domain or path, the only thing you can do is load a page for which the more-specific cookie is out-of-scope, and read it from there.

    If, as you say, you only need to remove a cookie, what you could do is try to remove the cookie at every possible level of specificity, eg.:

        document.cookie= 'foo=;domain=sub.domain.example.com;expires=Sat, 01-Jan-2000 00:00:00 GMT';
        document.cookie= 'foo=;domain=domain.example.com;expires=Sat, 01-Jan-2000 00:00:00 GMT';
        document.cookie= 'foo=;domain=example.com;expires=Sat, 01-Jan-2000 00:00:00 GMT';
    

    and similarly with the path variable. You could put this in a nested loop for each path and domain part, splitting on . for the domain and / for the path.

    0 讨论(0)
  • 2020-12-08 19:35

    You can only access cookies from the same domain (this includes subdomains). Obviously doing otherwise would be a security concern.

    0 讨论(0)
提交回复
热议问题