我的javascript cookie适用于我的节目和隐藏,但不能用于我的切换?

时间:2011-06-10 14:56:25

标签: javascript cookies

我有一个cookie,可以记住我的下拉是隐藏还是可见。然后我在下拉列表中添加了一个图像,并切换了照片,但cookie似乎不记得切换的状态。

<script type="text/javascript">
<!--
var state;
var stateTog;
window.onload = function () {
    obj = document.getElementById('featured');
    state = (state == null) ? 'hide' : state;
    obj.className = state;
    document.getElementById('featured-header').onclick = function () {
        var option = ['tel1', 'tel2'];
        for (var i = 0; i < option.length; i++) {
            objTog = document.getElementById(option[i]);
            objTog.className = (objTog.className == "visible") ? "hidden" : "visible";
        }
        obj.className = (obj.className == 'show') ? 'hide' : 'show';
        state = obj.className;
        stateTog = objTog.className;
        setCookie();
        return false;
    }
}
function setCookie() {
    exp = new Date();
    plusMonth = exp.getTime() + (31 * 24 * 60 * 60 * 1000);
    exp.setTime(plusMonth);
    document.cookie = 'State=' + state + ';expires=' + exp.toGMTString();
    document.cookie = 'StateTog=' + stateTog + ';expires=' + exp.toGMTString();
}
function readCookie() {
    if (document.cookie) {
        var tmp = document.cookie.split(';')[0];
        state = tmp.split('=')[1];
        stateTog = tmp.split('=')[1];
    }
}
readCookie();
//-->
</script>

2 个答案:

答案 0 :(得分:0)

readCookie函数中的此代码看起来有问题:

state = tmp.split('=')[1];
stateTog = tmp.split('=')[1];

我希望state和stateTog具有相同的值,这不一定是预期的。看起来代码应该循环遍历所有键/值对。

答案 1 :(得分:0)

这是一个更好的cookie脚本

用法:

    stateTog = objTog.className;
    setCookie("State",state,expiryDate);
    setCookie("StateTog",stateTog,expiryDate);


function readCookie() {
    state = getCookie("State");
    stateTog = getCookie("StateTog");

}

// cookie.js file
var daysToKeep = 14; // default cookie life...
theCookie = '';
today      = new Date(); 
expiryDate = new Date(today.getTime() + (daysToKeep * 86400000));

/* Cookie functions originally by Bill Dortsch */
function setCookie (name,value,expires,path,theDomain,secure) { 
   value = escape(value);
   var theCookie = name + "=" + value + 
   ((expires)    ? "; expires=" + expires.toGMTString() : "") + 
   ((path)       ? "; path="    + path   : "") + 
   ((theDomain)  ? "; domain="  + theDomain : "") + 
   ((secure)     ? "; secure"            : ""); 
   document.cookie = theCookie;
} 

function getCookie(Name) { 
   var search = Name + "=" 
   if (document.cookie.length > 0) { // if there are any cookies 
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value 
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value 
         if (end == -1) end = document.cookie.length 
         return unescape(document.cookie.substring(offset, end)) 
      } 
   } 
} 
function delCookie(name,path,domain) {
   if (getCookie(name)) document.cookie = name + "=" +
      ((path)   ? ";path="   + path   : "") +
      ((domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
//   alert(name+' marked for deletion');
}