我有要使用的可点击背景代码,但是当我单击背景时,它总是打开的。如何使此代码每天以最简单的方式每天工作一次……使用cookie或其他方法。我真的需要帮助。谢谢!
<body onclick="location.href='test.html';">
答案 0 :(得分:0)
您可以使用localStorage
。
<script>
function onBodyClick() {
var lastOpened = localStorage.getItem('body-opened'); // You can use another identifier instead of 'body-opened'
if (lastOpened && new Date(lastOpened).toDateString() === new Date().toDateString()) {
return true;
} else {
localStorage.setItem('body-opened', new Date().toDateString());
document.location.href = 'test.htm';
}
}
</script>
<body onclick="onBodyClick()"></body>
答案 1 :(得分:0)
如果要限制用户每天仅打开一次链接。您可以执行以下操作:
<body onclick="openLink()">
<script>
function openLink() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
// As date object returns time as well. Which we dont need. So we remove that.
if(localStorage.getItem('date') == today) {
alert('come back tomorrow');
} else {
localStorage.setItem('date', today);
location.href='test.html';
}
}
</script>