在目录上设置cookie [英] Setting cookies on directories
本文介绍了在目录上设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设置cookie来记住className更改,但是如果我设置cookie @ mydomain /它读取所有目录路径,但如果cookie设置在mydomain / sub /它只读取该目录路径。
I'm setting a cookie to remember a className change, however if I set the cookie @ mydomain/ it reads for all directory paths, but if the cookie is set at mydomain/sub/ it only reads for that directory path.
有任何建议吗?
HTML:
<html>
<head>
<script type="text/javascript" src="theme.js" /></script>
</head>
<body id="site-index" class="defaultTheme">
<a href="#" id="themeone">Theme one</a>
<a href="#" id="themetwo">Theme Two</a>
</body>
</html>
我的代码(theme.js):
My code(theme.js):
var stateTheme;
window.onload=function() {
objTheme=document.getElementsByTagName('body')[0];
stateTheme=(stateTheme==null)?' defaultTheme':stateTheme;
objTheme.className+=stateTheme;
//THEME
document.getElementById('themeone').onclick=function() {
objTheme.className='defaultTheme';
stateTheme=objTheme.className;
setCookie("StateTheme",stateTheme,expiryDate);
return false;
}
document.getElementById('themetwo').onclick=function() {
objTheme.className=(objTheme.className=='defaultTheme')?' silverTheme':' silverTheme';
stateTheme=objTheme.className;
setCookie("StateTheme",stateTheme,expiryDate);
return false;
}
}
//COOKIES
function setCookie() {
exp=new Date();
plusMonth=exp.getTime()+(31*24*60*60*1000);
exp.setTime(plusMonth);
document.cookie='StateTheme='+stateTheme+';expires='+exp.toGMTString();
}
function readCookie() {
stateTheme = getCookie("StateTheme");
}
// Cookie
var daysToKeep = 500; // default cookie life...
theCookie = '';
today = new Date();
expiryDate = new Date(today.getTime() + (daysToKeep * 86400000));
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 : "127.0.0.1") +
((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))
}
}
}
readCookie();
推荐答案
您有两个setCookie函数。
You have two setCookie functions.
删除此第一个
// COOKIES
//COOKIES
function setCookie() {
exp=new Date();
plusMonth=exp.getTime()+(31*24*60*60*1000);
exp.setTime(plusMonth);
document.cookie='StateTheme='+stateTheme+';expires='+exp.toGMTString();
}
并使用
setCookie(StateTheme,stateTheme,expiryDate,/);
其他setCookie函数我的代码 - 但你发布的cookie代码绝对是我放在一起:)
I do not who added that other setCookie function to my code - but the cookie code you posted is definitely one I put together :)
这篇关于在目录上设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文