[英]How can I set a 3rd party cookie in a script served from remote domain?
It may be easy, but I never worked with cookies and the issue confuses me a little. 这可能很容易,但我从来没有使用过cookies,这个问题让我感到困惑。
So let's say I have a script. 所以,假设我有一个脚本。 It is uploaded to remote CDN.
它被上传到远程CDN。 I am using it by getting it in
我正在使用它
<script src=http://link-to-script.com/script.js></script>.
Then, I load html code with those tags and run in on localhost so it gets the script from CDN. 然后,我使用这些标记加载html代码并在localhost上运行,以便从CDN获取脚本。 Now, I use some of the script's function that sets a cookie within it with document.cookie .
现在,我使用一些脚本的函数,使用document.cookie在其中设置cookie。 In this part I would like to have the cookie not be set on a localhost domain(which is a case right now), but on a domain that the script was served from(CDN).
在这部分中,我希望cookie不能在localhost域上设置(现在就是这种情况),但是在脚本服务的域(CDN)上。 I want to have 3rd party cookie instead of 1st party.
我想要第三方cookie而不是第一方。
What is the best possible way to do that? 最好的方法是什么? Could you please point me to right direction?
你能指点我正确的方向吗?
Hey you can refer Can a 3rd party js script write cookies? 嘿,你可以参考可以第三方js脚本写饼干吗?
To write third-party cookies (ie where the cookie is on the domain of the third party) requires that the cookies be sent in the headers of a download from that third party , and not written by JS code. 要编写第三方cookie(即cookie位于第三方域中),需要在第三方下载的头文件中发送cookie ,而不是由JS代码编写。
Write below code in request headers, MDN reference link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie 在请求标题中写下面的代码,MDN参考链接https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
Set-Cookie: <cookie-name>=<cookie-value>
You can only set cookie for current domain and it's sub-domains . 您只能为当前域及其子域设置cookie。
For-Example: 例如:
You can create cookie for a.mydomain.com
, b.mydomain.com
, c.mydomain.com
from mydomain.com
. 您可以从
mydomain.com
为a.mydomain.com
, b.mydomain.com
, c.mydomain.com
创建cookie。
You can't create cookie for mydomain.com
from anotherdomain.com
. 您无法从
anotherdomain.com
为mydomain.com
创建cookie。
Cross-Domain Cookies 跨域Cookie
But if you want to create cookie for another domain, You need to redirect to that particular URL and create cookie from that site. 但是,如果要为其他域创建cookie,则需要重定向到该特定URL并从该站点创建cookie。
OR 要么
If you want to create cookie for another domain, you can try this, 如果您想为其他域创建cookie,可以试试这个,
<img src='http://www.anotherdomain.com/createCookie.php'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.