1

It doesn't seem that JS provide any ability off the bat to grab the value of a cookie based on the domain, but does anyone have a suggestion as to how this can be done?

For example: There may be two cookies with the same name, but one is set globally across all pages (.example.com) and the other is set on a certain page (sub.example.com).

document.cookie

will only retrieve a string of all the cookie key:value pairs, with no association to the appropriate domain. ex:

"this_cookie=abc; that_cookie=xyz; this_cookie=123"

I would like to grab the cookie which is instantiated on the subdomain (this_cookie #2)

Dear1ofGdBear
  • 763
  • 2
  • 11
  • 25

2 Answers2

0

document.cookie does not expose the domain information as you can see for yourself.

Just an idea: Maybe you can store document.cookie temporarily, then override the cookie for the subdomain and compare again with document.cookie to see which has changed.

But to be honest: Can you not put the subdomain into the cookie name or value?

jayms
  • 3,128
  • 2
  • 9
  • 18
-1

You cannot. or there will be security vulnerabilities. When you setup the cookie on subdomain, you have to set "globally across all pages (.example.com)" so that you can retrieve it from the main domain.

Keyang
  • 1,548
  • 9
  • 16