6

Safari browser blocks 3rd party cookies by default.

I have site A (main site) that loads site B (framed site) in the iframe. Site B sets some cookies ( WHEN THE USER CLICK A BUTTON ), to function properly. These cookies are called 3rd party cookies, as they are not set by site A.

So I found this solution: https://github.com/vitr/safari-cookie-in-iframe

But in my case it doesn't work because to set the cookie, the user first has to click a button.

The framed.html is infact like this:

<!DOCTYPE html>
<html>
<head>
    <title>Framed Page</title>
</head>
<body style="background-color: lightcoral;">
<h2>This is the framed site</h2>

<div border="0" class="cookie_button" id="continue_button" onclick="SetCookie('cookieAcceptanceCookie','accepted',9999);">accept</div>
</body>
</html>

Is there a solution to my problem?

2
  • “But in my case it doesn't work because to set the cookie, the user first has to click a button.” - and how exactly is that keeping you from doing the same thing as described in that article …? Whether you automatically redirect to that other domain, or do it only when the click has happened - what’s the difference?
    – CBroe
    Jul 17, 2018 at 10:26
  • If you are concerned that it interrupts the flow too much, then you either need to do it upfront (even if the user should afterwards not be interested in that functionality), or you could do it via a popup maybe. That the 3rd-party domain is visited “as a 1st-party” is the important thing here; whether that is via a redirect in the current window, or in an outside context such as a popup, should not really matter. (Whether the popup blocker might give you more trouble in the specific situation might be a different question.)
    – CBroe
    Jul 17, 2018 at 10:28

2 Answers 2

4

The only workaround that worked for me is redirecting through the iframed domain once.

So, if you have abc.com and xyz.com is an iframe under abc.com, while landing to the abc.com, there needs to be an intermediate redirect through xyz.com only for the purpose of setting a blank cookie. Then Safari will allow setting a cookie from iframe for xyz.com.

I'm not sure if there's any other better workaround. However, this worked and served my purpose.

Thanks.

4
  • You saved my day.
    – Kubadev
    Dec 11, 2019 at 16:57
  • 1
    Its not working anymore on safari 13+. Its blocking cookies anyways . Dec 18, 2019 at 9:44
  • Have you found the solution to this problem by now? Please share the sample code. Jan 30, 2020 at 3:36
  • Yes Safari 13.1.1 is blocking this approach.
    – Mike Flynn
    Aug 18, 2020 at 21:03
2

Safari now blocks all third party cookies. You can only use the Storage API to try to get user access to their third party cookies.

https://www.infoq.com/news/2020/04/safari-third-party-cookies-block/

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.