简体   繁体   English

如何在从远程域提供的脚本中设置第三方cookie?

[英]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.coma.mydomain.comb.mydomain.comc.mydomain.com创建cookie。

You can't create cookie for mydomain.com from anotherdomain.com . 无法anotherdomain.commydomain.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.

相关问题 如何远程点击第三方网站上的链接 - How to remote click on links from a 3rd party website 如何通过 Ember 中的脚本标签供应 3rd 方 js 库? - How can I vendor a 3rd party js lib via a script tag in Ember? 我该如何插入<script type='text/javascript' src=''> tag on a 3rd party webpage using rails? - How can I insert a <script type='text/javascript' src=''> tag on a 3rd party webpage using rails? 从第三方脚本修改innerHTML - Modifying innerHTML from 3rd Party Script 我如何在 angular 中使用 3rd 方 jquery 插件? - how can i use 3rd party jquery plugins in angular? 试图嵌入使用的第三方Javascript <?js= value ?> ,我怎么能阻止PHP解析“ - Trying to embed 3rd party Javascript that uses <?js= value ?>, how can i stop php from parsing the “<?” 如何从现有的第三方控件链接jQuery事件? - How can I chain jQuery events from an existing 3rd party control? 通过小部件设置第 3 方 cookie,以便稍后识别用户 - Setting a 3rd party cookie through a widget so I can recognize the user later 从第3方域名中提取一个div以显示在我的网页上? - Pulling a div from 3rd party domain to display on my webpage? 我如何设置第三方cookie - How I can set third party cookie
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM