设置来自JavaScript的其他域的Cookie [英] Setting cookie for different domain from javascript

查看:130
本文介绍了设置来自JavaScript的其他域的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将cookie设置为与js文件的src相同的域。



方案:
www.xyz.com html,我已经包含来自qwe.com的js文件,如下所示:



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



我要从此b.js创建域设置为.qwe.com的cookie。我正在使用以下功能设置Cookie



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



使用上述代码,我无法设置cookie。
示例:www.flipkart.com->在开发者控制台的资源标签中检查cookie-> .scorecardresearch.com和.doubleclick.net能够设置cookie



<我想做同样的事情。有人可以分享解决方案吗?真正可行的解决方案。我已经通过Google搜索尝试了多种解决方案。

解决方案

客户端JavaScript只能为网页所在的域设置cookie。

>

您引用的示例使用 HTTP标头设置cookie ,而不是JavaScript。


I am trying to set cookie to domain same as src of js file.

Scenario: In www.xyz.com html, I have included js file from qwe.com as below

<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. I am setting cookie with following function

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. Example: www.flipkart.com-> Check cookies in resources tab of developer console-> .scorecardresearch.com and .doubleclick.net are able to set 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. It didn't work.

解决方案

Client side JavaScript can set cookies only for the domain the webpage is hosted on.

The examples you cite use HTTP headers to set cookies, not JavaScript.

这篇关于设置来自JavaScript的其他域的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆