Soroush Khosravi
Soroush Khosravi

Reputation: 190

send cookies to another web page with javascript

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.

Upvotes: 4

Views: 11600

Answers (3)

Razor
Razor

Reputation: 17518

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.

Upvotes: 1

mahemoff
mahemoff

Reputation: 46509

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/

Upvotes: 1

Qorbani
Qorbani

Reputation: 5905

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

Upvotes: 1

Related Questions