The goal of this code is to set a cookie for the background color of the page. I don't understand why the cookie won't set.
<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>
Thanks in advance!
在您的setColorCookie()
函数中,您有一个exdays
的变量exdays
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.