Possible to detect if a specific third-party cookie is allowed in the browser?

Viewed 682

Is it possible to detect if a cookie from a specific external domain is allowed by the browser to be read by the visitor of my site? This domain, which I don't control, is displayed in an iframe, however it won't work unless it has access to its cookie.

When third-party cookies are enabled or the site is added to Allow section in the settings, it works okay. I would like to display a user friendly error message when this is not the case.

2 Answers

A better approach would be to send the cookie content to the iframe using the poatMessageAPI.

That way you are not requiring the user to reconfigure their browser to be less secure

One can access the browser's cookies with document.cookie and an iframe's document with iframe.contentDocument (where iframe is a reference DOM node).

With that one should be able to check (just after iframe had loaded) if a cookie exists:

iframe.contentDocument.addEventListener("DOMContentLoaded", () => {
  if (iframe.contentDocument.cookie.split(';').find((item) => item.includes('cookie='))) {
    console.log("Cookie exists");
  }
}
Related