简体   繁体   English

从JavaScript设置不同域的Cookie

[英]Setting cookie for different domain from javascript

I am trying to set cookie to domain same as src of js file. 我正在尝试将cookie设置为与js文件的src相同的域。

Scenario: In www.xyz.com html, I have included js file from qwe.com as below 场景:在www.xyz.com html中,我包括了来自qwe.com的js文件,如下所示

 <script type="application/javascript" src="http://qwe.com/b.js"></script> 

From this b.js, i want to create cookie with domain set to .qwe.com. 从这个b.js,我想创建域设置为.qwe.com的cookie。 I am setting cookie with following function 我正在使用以下功能设置Cookie

 function createCookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); } else { var expires = ""; } document.cookie = name+"="+value+expires+";domain=.qwe.com"+"; path=/;"; } 

With above code I am unable to set cookie. 使用上面的代码,我无法设置cookie。 Example: www.flipkart.com-> Check cookies in resources tab of developer console-> .scorecardresearch.com and .doubleclick.net are able to set cookie 示例:www.flipkart.com->在开发者控制台的资源选项卡中检查cookie-> .scorecardresearch.com和.doubleclick.net能够设置cookie

I want to do same. 我也要这么做 Can someone please share solution for this? 有人可以分享解决方案吗? Real working solution. 真正可行的解决方案。 I have tried multiple solutions by doing Google search. 我已经通过Google搜索尝试了多种解决方案。 It didn't work. 没用

Client side JavaScript can set cookies only for the domain the webpage is hosted on. 客户端JavaScript只能为托管网页的域设置cookie。

The examples you cite use HTTP headers to set cookies , not JavaScript. 您引用的示例使用HTTP标头设置cookie而不是JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM