0

Scenario:

I have parent site A domain A that embeds an iframe with domain B...note that I have control of both sites. Im calling postMessage from site A that posts some information to be stored as cookie in iframe domain B. The iFrame will listen to the postMessage event and set cookie accordingly.

Domain A:

 var frame = document.getElementById('exampleFrame');
 frame.contentWindow.postMessage({"age":28}, '*');

Embedded iframe domain B:

window.addEventListener('message', messageReceivedEvent, false);

function messageReceivedEvent(e) {
   document.cookie("age=" + e.data.age + ";");
   console.log(document.cookie);
}

console.log(document.cookie) returns nothing

The issue here is that, once i set the cookie in the messageReceivedEvent, it seems like the cookie is not stored in domain B. Did I missed out something?

Note: This issue only happened in latest Google Chrome version, Firefox works fine.

Pow4Pow5
  • 501
  • 2
  • 8
  • 22

1 Answers1

0

Chrome handles cookies differently. See Mozilla Document.cookie Example #1:

  • it works perfectly in Firefox
  • does not work in Chrome

See this SO thread about chrome cookies.

Also, with iFrames other people reported this Chrome "bug" here:

Not sure how to solve it, but maybe use localStorage instead?

verjas
  • 1,793
  • 1
  • 15
  • 18