Check if third-party cookies are enabled

前端 未结 6 1403
既然无缘
既然无缘 2020-11-27 10:58

I have an application that needs to check whether the client browser has third-party-cookies enabled. Does anyone know how to do this in JavaScript?

6条回答
  •  孤独总比滥情好
    2020-11-27 11:23

    Third Party Cookie detection with Whitelisting of URLs

    Alan H & Gojko Adzic are good enough for majority of use cases, but these solutions won't work if you want your users to do whitelisting of third party cookies only to certain domains.

    I'm presenting a slightly modified version of Gojko Adzic's answer

    For this we need two domains :

    • Domain 1, this is the page your user lands, it initially sets tpc=pending, then it redirects to Domain 2
    • Domain 2 injects Domain 1's url in an iFrame, tries to set a cookie tpc=true and redirects back to Domain 1
    • Now, Domain 1 reads the cookie tpc and checks if its true, if we get value as true, third party cookies are allowed if it is still in pending third party cookies are blocked blocked.

    Now, you might ask your users to whitelist (allow third party cookies) ONLY for Domain 1, also by this technique third party detection will be accurate if the users had white listed your domain.


    This is tested in Chrome 74, 75, 76 & Edge 78

    Unfortunately, Mozilla doesn't provide whitelisting of urls like Chrome does and Safari has it own mechanisms of detecting third party cookies (ITP).

    P.S. Will upload this demo in my github when I get time.

提交回复
热议问题