You can read cookies on a subdomain of the same domain, but never on completely different domains. This is completely intentional and if it was possible to circumvent it would open a floodgate of privacy issues. The way you would normally get around this is to pass a session id or other authentication token via URL to the other domain, but if you cannot use server-side code that will not be secure.
Cross Domain Cookies with jQuery and jQuery Cookie
-
28-06-2023 - |
Question
I have a website that open a modal upon visiting the site. The person has a few options with the modal. If they choose a certain option a session cookie is created.
Here is the code I am using to check for the cookie and run a function based on that:
$(function() {
if($.cookie('my-cookie') === undefined) {
$('#modal').foundation('reveal', 'open');
}
$( ".close-modal" ).click(function() {
$('#modal').foundation('reveal', 'close');
$.cookie('my-cookie', '1', {path:'/'});
});
});
I also have another domain that I would like to check if this cookie exists. From my understanding there is no way to read another domains cookies, even if I own and control that domain.
My question is this: Is there a workaround or a way that would allow me to check if a cookie exists from another domain? Please keep in mind I do control both domains and can upload code to either or both domains.
Also I use a hosted CMS so I cannot use server side code. I am using jQuery Cookie on both domains and using jQuery 2.1.
Solution