Setting cookie for different domain from javascript

时光总嘲笑我的痴心妄想 提交于 2020-07-21 05:19:50

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31074620/setting-cookie-for-different-domain-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!