It is required to keep the cookie for all subdomains, but it does not work.

What's wrong? Code:

function cetcooke(status) { var date = new Date; var SERVER_NAME = ".site.ru"; date.setDate(date.getDate() + 15); // врСмя ΠΆΠΈΠ·Π½ΠΈ ΠΊΡƒΠΊΠΈ document.cookie = "sound="+ status +"; expires="+date.toUTCString()+"; path='"+SERVER_NAME+"'"; // ставим ΠΊΡƒΠΊΡƒ } 

I click on the button saves as for a specific page, but not for the domain.

Help solve the problem.

    1 answer 1

    Wrong data is written in the path. It is necessary so:

     path="/" 

    And the fact that you have in the variable var SERVER_NAME = ".site.ru"; need to write to the domain

    Here I always have a function to create cookies:

     function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ) { var cookie_string = name + "=" + escape ( value ); if ( exp_y ) { var expires = new Date ( exp_y, exp_m, exp_d ); cookie_string += "; expires=" + expires.toGMTString(); } if ( path ) cookie_string += "; path=" + escape ( path ); if (domain) cookie_string += "; domain=" + escape (domain); if ( secure ) cookie_string += "; secure"; document.cookie = cookie_string; } 

    Here so I use:

     set_cookie('nameCookie', "valueInCookie", 0, 0, 0, "/"); 
    • helped, thanks. - Alexander Sizintsev