此代码的目标是为页面的背景色设置cookie。我不明白为什么无法设置Cookie。
<select id="ddlTheme" onchange="setColorCookie()">
<option value="Select Color">Select Color</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
<script>
window.onload = function() {
if(document.cookie.length != 0) {
var nameValueArray = document.cookie.split("=");
document.getElementById("ddlTheme").value = nameValueArray[1];
document.body.style.backgroundColor = nameValueArray[1];
console.log("Window onload happened sir!");
}
}
function setColorCookie() {
var selectedValue = document.getElementById("ddlTheme").value;
if (selectedValue != "Select Color") {
document.body.style.backgroundColor = selectedValue;
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = "color=" + selectedValue + ";" + expires;
console.log("Set cookie");
}
}
</script>
谢谢!
答案 0 :(得分:1)
在setColorCookie()
函数中,您有一个未定义的变量exdays
。