Ваш $.cookie('state')
выглядит как строка ("false
").
В качестве доказательства см. Следующий код (также в this jsfiddle ):
var current_state='false';
if(current_state==null) {
current_state=false;
}
console.log(current_state);
var current_state=(!current_state);
console.log(current_state);
Выводит false
дважды.
Почему? Потому что вы проверяете, является ли оно пустым, и не поддерживаете другие случаи.
Вы можете сделать что-то вроде этого:
var current_state='false';
if(current_state==null ||
current_state==false ||
current_state=='false') {
current_state = false;
}else{
current_state = true;
}
console.log(current_state);
current_state=(!current_state);
console.log(current_state);
и ваш код сначала выведет логическое true
или false
, а затем противоположное логическое значение.