jensiepoo
jensiepoo

Reputation: 579

Unable to set cookies in Javascript

I'm trying to set my cookies in Javascript the following way

<video id = 'media'></video>

document.addEventListener('DOMContentLoaded', function() { 
   document.cookie='X-At=$ACCESS_TOKEN$';
   document.getElementById('media').src = "$some video link$";
});

However, the cookie just doesn't seem to be added. The file is also hosted on a simpleHttpServer because Chrome seems to ignore pages on local pages. Could someone tell me where the problem is? Note that the cookie gets set when I delete document.getElementById('media').src = "$some video link$";... SoI'm guessing it has to do with setting the source of the element.

Thanks so much.

Upvotes: 0

Views: 1225

Answers (2)

jensiepoo
jensiepoo

Reputation: 579

Found out that cookies are domain specific. So I was unable to set the cookies for the request to a different network, i.e. my file is hosted on a local network 127.0.0.1 and I was trying to send the cookie to a different domain.

The hacky go around for this was to create a proxy server for my request and overwrite the cookie there and redirect the target to my original destination. Couldn't think of a better way to do this.

Upvotes: 1

Kevin Simple
Kevin Simple

Reputation: 1213

mate: way of create a cookie like this

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

Upvotes: 0

Related Questions