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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

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.
I meant we cannot change or sharing cookies from one domain to another one, but regarding sending them you absolutely right :-)
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/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.