且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在不同的域上设置 cookie,使用 javascript 或其他

更新时间:2022-05-09 00:06:04

您可以使用回调将脚本元素注入到文档的 HEAD 中,该回调将您需要的 cookie 传递给任何需要它的函数.

You could inject a script element into HEAD of the document with a callback that passes the cookie you need to whatever function needs it.

类似于:

 <script type="text/javascript">
   var newfile=document.createElement('script');
   newfile.setAttribute("type","text/javascript");
   newfile.setAttribute("src", 'http://first.com/doAjax?getCookie&callback=passCookie');
   document.getElementsByTagName("head")[0].appendChild(newfile);
 </script>

页面 first.com/doAjax?getCookie 可以这样做:

And the page first.com/doAjax?getCookie could do this:

     passCookie({'name':'mycookie', 'value':'myvalue'});