<html>
<head>
<title>利用Cookies实现更换主题</title>
</head>
<body>
<script type="text/javascript">
function changeBgColor() {
this.setCookies = function(name) {
// 设置过期时间是1天
var now = new Date();
now.setDate(now.getDate() + 1);
document.cookie = name + "=1;expires=" + now.toGMTString() + ";";
},
this.getCookies = function(name) {
var idx = document.cookie.indexOf(name + "=");
if (idx != -1) {
var edx = document.cookie.indexOf(";",idx+name.length);
var result = document.cookie.substring(idx,edx);
return result;
}
return '';
},
this.changeBGColor = function(name) {
var ck = this.getCookies(name);
if (ck != '') {
alert("1天内只能更换1次主题");
}
else {
this.setCookies(name);
document.body.style.backgroundColor = 'gray';
}
},
this.removeCookie = function(name) {
// 将过期日期设置为一个过去的时间,清除Cookie
var now = new Date();
now.setDate(now.getDate() - 2);
document.cookie = name + "=1;expires=" + now.toGMTString() + ";";
alert("cookie已清除,可以重新设置主题!");
}
}
</script>
<input type="Button" value="Change backgroundColor" onClick="javascript:new changeBgColor().changeBGColor('changeColor');">
<input type="Button" value="remove cookies" onClick="javascript:new changeBgColor().removeCookie('changeColor');">
</body>
</html>
利用Cookies实现更换主题
最新推荐文章于 2021-09-17 13:52:24 发布