正如标题所暗示的那样,除了Chrome之外,这种方法无处不在,如果可能的话,我想修复它。用户可以从下拉框中选择一种颜色,并将其保存在cookie中,以便下次加载。但在Chrome中,下拉框无效。代码传递了Validator文本。
无论如何,如果你能提供帮助,那就太好了。
<html>
<head>
<meta http-equiv="Content-Language" content=en-us>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Background Color selection</title>
</head>
<body>
<form onsubmit="return false;" action="">
<p><select name="color_select" size="1" onchange="return setColor(this, global_name)">
<option style="background-color: aliceblue;" value="aliceblue">aliceblue (#F0F8FF)</option>
<option style="background-color: antiquewhite;" value="antiquewhite">antiquewhite (#FAEBD7)</option>
<option style="background-color: aqua;" value="aqua">aqua (#00FFFF)</option>
<option style="background-color: aquamarine;" value="aquamarine">aquamarine (#7FFFD4)</option>
<option style="background-color: azure;" value="azure">azure (#F0FFFF)</option>
<option style="background-color: beige;" value="beige">beige (#F5F5DC)</option>
</select>
<input type="button" value="Delete Cookie"
onclick="delCookie(global_name); return getColor(color_select, global_name)"></p>
</form>
<script type="text/javascript">
<!--//
var global_name = 'color';
//
function getColor(sel, cookie_name) {
var cookie_value = getCookie(cookie_name);
if (!cookie_value) cookie_value = 'white';
document.body.style.backgroundColor = cookie_value;
var opt = sel.options;
var x, len = sel.length;
for (x=0; x<len; x++) {
if (opt[x].value == cookie_value) {
opt[x].selected = true;
break;
}
}
return true;
}
getColor(document.forms[0].color_select, global_name);
//
function setColor(sel, cookie_name) {
var opt = sel.options[sel.selectedIndex].value;
var oneDay = 24 * 60 * 60 * 1000;
var oneYear = 365 * oneDay;
var expDate = new Date();
expDate.setTime(expDate.getTime() + oneYear);
setCookie(cookie_name, opt, expDate);
return getColor(sel, cookie_name);
}
// ---------------------------------------- //
function getCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function setCookie(name, value) {
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function delCookie(name) {
exp = new Date();
exp.setTime(exp.getTime() - (24*60*60*1000));
var cval = getCookie(name);
cval = (cval == null) ? "" : cval;
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
//-->
</script>
</body>
</html>
答案 0 :(得分:3)
正如Germán Enríquez's jsfiddle所示,该代码适用于Google Chrome(至少在版本24.0 beta中)。
我可以建议两件事:
另外,您说代码已通过验证。首先,HTML页面需要一个doctype,很抱歉,但它没有通过。您可以在此处自行尝试:http://validator.w3.org/#validate_by_input
另外,尝试在http://www.jslint.com/处抓取您的JavaScript代码,您会惊讶地发现代码中会出现多少问题。例如,var argv = setCookie.arguments;
不是访问函数参数的最佳方法,并且肯定会在严格模式下中断。此外,您的exp = new Date();
语句会将exp
变量泄漏到全局范围。
如果这没有帮助,请提供有关该问题的更多详细信息,我们将做出相应的回答。
我刚刚注意到您在评论中提到您将HTML复制到文件中并在浏览器中打开它。您是否使用file://
访问了自己的文件?如果是这样,那可能就是Chrome导致您出现问题的原因。实际上,Google Chrome:
...故意在file://上禁用cookie,原因各种各样......
自2008年以来,这是Chrome中的 WontFix问题535 ,请参阅http://code.google.com/p/chromium/issues/detail?id=535
尝试运行本地服务器并通过http://localhost/
或http://127.0.0.1/