4

How can I send cookie on a page, to another page with JS?
For example I have two pages:
1 - www.domain1.com/admin.php
2 - www.domain2.com/getCookies.php
How can I send cookies from admin.php to getCookies.php and get them in this form :
getCookies.php?name=x&val=y
x is cookie name and y is the value of x.

Soroush Khosravi
  • 180
  • 1
  • 2
  • 13

3 Answers3

1

Use cross-document messaging (embed domain2 in an iframe and communicate between domain1 website and domain2) if you want to do this in the browser, or if you can call domain2's server (and control it), you can use Cross-Origin Resource Sharing.

You'll find several libraries to make this kind of communication happen inside the browser at the top of this blog post (my post) if you want it to work for older browsers: http://softwareas.com/cross-domain-communication-with-iframes

If you're not worried about older browsers, you just need to send the cookie data along: document.getElementById("iframe").contentWindow.postMessage(cookieData, 'domain1.com');

See John Resig's post for more details: http://ejohn.org/blog/postmessage-api-changes/

mahemoff
  • 44,526
  • 36
  • 160
  • 222
1

You can not send cookies to different domain. Take a look at this for more detail:

Community
  • 1
  • 1
Qorbani
  • 5,825
  • 2
  • 39
  • 47
  • Of course this can be done, you simply need to read in the cookie values, serialize it to a string and send it using JSONP. Not the most elegant solution though. – Razor Jan 03 '12 at 10:39
  • I meant we cannot change or sharing cookies from one domain to another one, but regarding sending them you absolutely right :-) – Qorbani Jan 03 '12 at 15:57
1

I have done this in the past using JSONP. It will work in all browsers.

Simply read in the cookie values, craft a JSON string and send it across.

See http://www.ibm.com/developerworks/library/wa-aj-jsonp1/ for some examples.

Razor
  • 17,271
  • 25
  • 91
  • 138