I want to open new window by using window.open and I want to set new cookie for that new window . I have tried to set cookie by using below code
window.open("https://mysite.n")
var expires = (function(days){
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 *
1000));
return date.toUTCString();
})(5);
document.cookie ="mytestcookie=testcookie; expires="+expires+"; path=/";
After this code execution . When I tried to get this cookie in new window I am getting undefined . Please help to set cookie in new window .
Thanks in Advance .
I want to open new window by using window.open and I want to set new cookie for that new window . I have tried to set cookie by using below code
window.open("https://mysite.n")
var expires = (function(days){
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 *
1000));
return date.toUTCString();
})(5);
document.cookie ="mytestcookie=testcookie; expires="+expires+"; path=/";
After this code execution . When I tried to get this cookie in new window I am getting undefined . Please help to set cookie in new window .
Thanks in Advance .
Share Improve this question asked Aug 22, 2018 at 10:59 silvesterprabusilvesterprabu 1,45710 gold badges31 silver badges46 bronze badges2 Answers
Reset to default 11You need to set the cookie before you load the new page. Just reorder your code so window.open
is last.
This assumes that new page is on the same origin as the current page. If it isn't, you can't set a cookie. It would be a major security problem if you could record data in a datastore belonging to someone else's website.
Quentin is absolutely right, but there is a way if you really have to achieve this just use a Headless Chromium with for example Puppeteer. You can go to the page and afterwords set the Cookies.