开发者

my javascript cookie works for my show and hide, but doesn't work for my toggle?

I have a cookie that remembers if my drop down is hidden or visible or not. Then I added a image to the drop down, and toggling the the photos also, but the cookie doesn't seem to remember what state the toggle is in.

<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 * 6开发者_StackOverflow社区0 * 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>


Here is a better cookie script

Usage:

    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');
}


This code in the readCookie function looks problematic:

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

I would expect state and stateTog to have the same value, which is not necessarily what is intended. It looks like the code should loop through all the key / val pairs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜