最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

send cookies to another web page with javascript - Stack Overflow

programmeradmin4浏览0评论

How can I send cookie on a page, to another page with JS?
For example I have two pages:
1 - www.domain1/admin.php
2 - www.domain2/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.

How can I send cookie on a page, to another page with JS?
For example I have two pages:
1 - www.domain1./admin.php
2 - www.domain2./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.

Share Improve this question asked Jan 1, 2012 at 17:46 Soroush KhosraviSoroush Khosravi 1901 gold badge2 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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./developerworks/library/wa-aj-jsonp1/ for some examples.

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

  • How do I share cookies across 2 domains with javascript?
  • What's your favorite cross domain cookie sharing approach?

Use cross-document messaging (embed domain2 in an iframe and municate 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 munication happen inside the browser at the top of this blog post (my post) if you want it to work for older browsers: http://softwareas./cross-domain-munication-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.');

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

发布评论

评论列表(0)

  1. 暂无评论