Setting cross-domain cookies in Safari

I have to call domain (which sets the cookies with http) from domain All I do on domain is (javascript):

var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.src = "";

This sets the cookie on on every browser I've tested, except Safari. Amazingly this works in IE6, even without the P3P headers.

Is there any way to make this work in Safari?



From the Safari Developer FAQ:

Safari ships with a conservative cookie policy which limits cookie writes to only the pages chosen ("navigated to") by the user. This default conservative policy may confuse frame based sites that attempt to write cookies and fail.

I have found no way to get around this.

If it's worth anything, Chrome doesn't set the cookies either if you use the <script> appending method, but if you have a hidden <img> with the same source, Chrome works in addition to the rest of the browsers (except, again, Safari)


Working method 2014-2016:

You have to do to the domain / assign a cookie / close the popup, the domain is now safelisted.

Original post @ PHP multiple cookies not working on iPad / iPhone browser


There is a bit of an evil trick assuming they have flash installed.

I'm not sure if it still works or not, but Flash'es "Local Shared Objects" aka Flash Cookies could help you circumnavigate Safari's same-domain policies.

Local Shared Object Tutorial

However, it may be complicated to implement, to say the least.

Additonally, LSO's are comming into the light as being a security nightmare:

So think carefully before using them.


This might not work for everyone, but I came across this issue because I was serving a React App from a different host than the API, and the solution that ultimately worked was to use DNS:

Our client was being served from and our API was on By making a CNAME record -->, and having our client use that subdomain for API calls, Safari stopped considering it a "third-party" cookie.

The upside is that there's very little code involved, and it's all using well-established stuff... The downside is that you need some control/ownership over the API host if you're going to use https - they need a certificate that's valid for the client domain, or users will get a certificate warning - so this wouldn't work (at least not for something end-user-facing) if the API in question isn't yours or a partner's.


A post to a hidden <iframe> can allow you to by-pass this restriction in Safari --

  header('P3P: CP=HONK');
  setcookie('test_cookie', '1', 0, '/');
<div id="test_cookie" style="position: absolute; top: -10000px"></div>
  window.setTimeout(function() {
    if (document.cookie.indexOf('test_cookie=1') < 0) {
        name = 'test_cookie',
        div = document.getElementById(name),
        iframe = document.createElement('iframe'),
        form = document.createElement('form'); = name;
      iframe.src = 'javascript:false';

      form.action = location.toString();
      form.method = 'POST'; = name;

  }, 10);

There is a proper workaround for this working in 2015. Let's say there is website which includes iframe with site The iframe wants to store a cookie. That is not permitted by Safari policy, however, is able to store it. So must listen to messages from and then store the cookie itself.

var _cookieEvMth = window.addEventListener ? "addEventListener" : "attachEvent";
var _cookieEvAction = window[_cookieEvMth];
var _cookieEv = _cookieEvMth == "attachEvent" ? "onmessage" : "message";
_cookieEvAction(_cookieEv, function(evt){
    var datack ='|');

When needs to store the cookie, it must post a message to


Also you can work your way to post message to the iframe if you want to read the cookie. Or you can include it as parameter in iframe url using javascript:


A workaround we just came up with at my job was to set the cookie via a - it may not be optimal for you (as you'll have an ugly ass popup window open), but it worked well for us. We had to have a popup window open anyway for OAuth authentication.

So the jist of what we did was:

  1. User clicks a link from
  2. Popup window opens to
  3. sets its cookie, and then redirects to in the proper place

Again, not valid in all solutions, but it worked in ours. Hope this helps.


I know this question is rather old, but this helped me to solve cookies problem:

var cookieForm = document.createElement("form");
cookieForm.action = "";
cookieForm.method = "post";


The idea to make a form post on a page that sets your cookies.


*EDIT* This workaround has been reported closed in WebKit.


Ok, so this answer is two years old, but... you can set a cookie from an iframe if you post a form to a hidden iframe. You can do this by creating a form:

<form id="myiframe" action="" method="POST" target="iframe_target">

Then in Javascript, get a reference to the form and call submit:


You can listen to the iframe's onload, or you can have your iframe action page issue some javascript that signals the load. I have tested this in Safari and Chrome, and it works.



Perhaps pragmatically create and click a link with an href="" and a target attribute pointing to a hidden iframe. That may bypass Safari's policy of user navigation for setting cookies (I don't have Safari handy to test.)


I did some extensive investigation around this when I was trying to deploy a site that used Windows Live ID, which depended on the ability to be able to set 3rd party cookies in order to log out. It just... didn't work. Nothing we could do would get it to work. The Live ID team also did extensive investigation and their answer was "can't make it work".


Note this line:

script.src = "";

I could not get this working until I added the http, i.e.

script.src = "";

I found a simple solution. You just need for first time setting cookie to check if request come from the same origin or not, if not as usual you need to return into iframe a script that will repeat this request, already having permission to assign cookie. After that you can do other request directly through iframe accessing this cookie. This helped me in my tracking system. Try, this works well.


Its worth noting that this restriction in Safari doesn't apply across subdomains. So if you directly visit, then you can set cookies from without direct user interaction (iframe/JavaScript).

This was relevant for my case when developing an API. If you're visitors are arriving at, and then you want some JavaScript to interact with your API, then if the API is hosted at, then it will work on Safari.


Place this JavaScript on the page making cross-domain requests,

  var gup = function(name, url) {
     if(!url) url = location.href;
     name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
     var regexS = "[\\?&]"+name+"=([^&#]*)";
     var regex = new RegExp( regexS );
     var results = regex.exec( url );
     return results == null ? null : results[1];
  var isSafari = navigator.vendor && navigator.vendor.indexOf('Apple') > -1 && navigator.userAgent && !navigator.userAgent.match('CriOS');
  var n = gup("activated");
  if(isSafari && n == null) {
     //browser is Safari and cookies have not yet been activated
     var current_url = location.protocol + '//' + + location.pathname;
     var query_string = '?callback=' + encodeURIComponent(current_url + '?activated=1');
     var new_url = '' + query_string;
     window.location.href = new_url;
  //the rest of your code goes here, and you can now set cross-domain cookies on Safari

Then create a file on the other server, which needs to set cookies,

  if(isset($_GET['callback'])) {
     header('Location: '.$_GET['callback']);
  } else {
     //in case callback param is not set, simply go back to previous page
     echo "<script>";
     echo "window.history.back();";
     echo "</script>";

Here's how this works:

  1. When is first visited, a check is made to see whether the browser is Safari and whether a GET parameter of the name "activated" does not exist. If both conditions are met (which will happen on the first visit for a Safari browser), then the browser is redirected to with a GET parameter, "callback", containing the calling URL appended with an "activated" parameter.

  2. simply redirects back to the URL contained in the GET parameter, "callback".

  3. When http://example1.index.html is now hit this second time after being redirected-to, the GET parameter, "activated" will now be set, so the conditional from step 1 will not execute, thus allowing the script to continue execution.

This fulfills Safari's requirement of having the browser visit the 3rd party domain at least once in order to start setting cookies.


Try something like:

var w ="");

It may bypass safari's security policy.


It isn't the missing type-attribute thats annoying you ?-)

<script type="text/javascript">
  var head = document.getElementsByTagName("head")[0];
  var script = document.createElement("script");
  script.src = "";


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.