Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set cookie in new window?

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 .

like image 671
silvesterprabu Avatar asked Oct 16 '25 14:10

silvesterprabu


1 Answers

You 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.

like image 181
Quentin Avatar answered Oct 18 '25 11:10

Quentin