Javascript-форум (https://javascript.ru/forum/)
-   Javascript под браузер (https://javascript.ru/forum/css-html/)
-   -   добавить и изменить cookie (https://javascript.ru/forum/css-html/63486-dobavit-i-izmenit-cookie.html)

eridan 10.06.2016 08:50

добавить и изменить cookie
 
Здравствуйте, существует вот такая функция, которая отключает или включает звук по клику:

soundButton.click(function() {
 
  if (sound == 0) {
		sound = 1;
		document.getElementById("sound").style.backgroundPosition = "0px";
		audioLink.play();

	} else {
		sound = 0;
		document.getElementById("sound").style.backgroundPosition = "-40px";
	}
 });


Хочу добавить бессрочные куки для переменной sound, без куков все работает замечательно, но с этим кодом sound показывает значение 0 всегда, при чем звук включен и не отключается.

soundButton.click(function() {
	if(!sound) sound = 1;
 
  if (sound == 0) {
		document.cookie = "sound=1";
		document.getElementById("sound").style.backgroundPosition = "0px";
		audioLink.play();

	} else {
		document.cookie = "sound=0";
		document.getElementById("sound").style.backgroundPosition = "-40px";
	}
 });


Помогите добавить, спасибо.

Alex_63 10.06.2016 11:14

eridan,
function setcookie(a,b,c) {if(c){var d = new Date();d.setTime(d.getTime()+c);}if(a && b) document.cookie = a+'='+ encodeURIComponent(b) +(c ? '; expires='+d.toUTCString() : '');else return false;}
function getcookie(a) {var b = new RegExp(a+'=([^;]){1,}');var c = b.exec(document.cookie);if(c) c = c[0].split('=');else return false;return c[1] ? decodeURIComponent(c[1]) : false;}

soundButton.click(function() {
  if(!sound) sound = 1; 
  if (sound == 1) {
	setcookie("sound","1",30*3600*24*30*1000);
	document.getElementById("sound").style.backgroundPosition = "0px";
	audioLink.play();
  } else if (sound == 0) {
	setcookie("sound","0",30*3600*24*30*1000)
	document.getElementById("sound").style.backgroundPosition = "-40px";
  }
});

$(function () {
    if (getcookie("sound") == "1") audioLink.play()
});

eridan 10.06.2016 12:28

Спасибо, что помогаете, но теперь звук не отключается, кука sound всегда 1


Часовой пояс GMT +3, время: 10:40.