简体   繁体   中英

Set cross domain cookie using javascript

Hello I want to set crossdomain cookie using javascript.

So on domain localhost I can easily set cookie. So I load the page on localhost:300 and in the Browser's console I type:

document.cookie = "my_cookie=works; Domain=localhost; path=/;" 

This method works - I can see the cookie being set.

But now, I want to set cookie to a different domain from the same place:

document.cookie = "my_cookie=doesntwork; Domain=.hubspot.com; path=/;" 

And this time it doesnt work - I still have the old cookie value and domain:

在此处输入图像描述

I know that what I am trying to do is possible because I have seen it on many sites. For example if check the cookies on this site you will see many cookies with domain google but the domain is stackoverflow.com .

For example: 在此处输入图像描述

So, how can I imitate/mimic this? I see some suggest using iframes - I tried without success - the cookies are still blocked. Please provide workable example (I believe 5 lines of code can do )

I want to open localhost page in my browser and run JS code that would put cookie with domain like.example.com. This is obviously possible. But I cant do it. I'm not trying to complete some job here - I just want to understand how the cool kids do it.

You cannot set a "cross-domain" cookie for security reasons. If this was possible, you'd be able to remotely sign people out of their accounts on other websites if they visited yours by overwriting their cookie for their site. The cookies you are seeing on pages like StackOverflow are because that page includes resources from that server. For example, you can see imgur cookies on this page since the image you linked to is hosted there.

SORRY FOR MY BAD ENGLISH!

There is no such thing as a cross-domain cookie that said you can get the same tracking functionality with sharing a cookie.

for example we want to track a user in our ad network across affiliated sites.

our tracking endpoint is: https://tracking.a.com here we set a deviceid in the cookies. Our Affiliated Webmaster b.com adds a javascript which adds an iframe to the page. iframe loads somecontent from a.com hence the cookies are shared then you can inside the iframe send the users current location ( b.com/shop ) to a tracking endpoint and attach deviceid cookie which can only be accessed by webpage on a.com. now deviceid or user can be related to the affiliate website and their intrests can be pridicted - but it is illegal - and safari deletes these cookies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM