Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  179] [ 2]  / answers: 1 / hits: 18994  / 9 Years ago, fri, june 26, 2015, 12:00:00

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.


More From » cookies

 Answers
16

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.


[#66032] Thursday, June 25, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Jun 25, 21, 00:00, 3 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
;