2011-06-10 32 views
0

我有一个cookie可以记住我的下拉菜单是隐藏的还是可见的。然后我加入到降的图像下来,拨动照片也有,但是该cookie似乎不记得切换处于什么状态我的JavaScript 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> 
+0

如果你想一些帮助,你将需要对您的问题展开:什么**具体**你有问题吗?你有什么尝试?你期望发生什么? – 2011-06-10 14:59:23

+0

我只是添加了一些信息,请让我知道,如果你需要更多 – okMonty 2011-06-10 15:09:11

回答

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

我明白我在Cookie中使用这个js,但是使用代码如何使用它来让我的cookie工作我对JavaScript很新颖..谢谢你的回复 – okMonty 2011-06-10 15:22:10

+0

@okMonty - 我可能错过了你的评论。你解决了你的问题吗? – mplungjan 2011-06-23 09:59:05

+0

是的,一切都很完美,非常感谢哟 – okMonty 2011-08-12 20:46:39

0

此代码在readCookie函数如下问题:

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

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

相关问题