[英]HTML, open link click anywhere on page
I have code for clickable background that I want to use but when I click on background its always open. 我有可以使用的可点击背景的代码但是当我点击背景时它始终打开。 How to make this code to work one per day BUT at most easiest way possible... with cookie or something else.
如何使这个代码每天工作一次,但最简单的方式可能...用cookie或其他东西。 I really need help with this.
我真的需要帮助。 Thanks!
谢谢!
<body onclick="location.href='test.html';">
You can use localStorage
. 您可以使用
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>
If you want to restrict the user to open the link only once per day. 如果您想限制用户每天只打开一次链接。 You can do something like this:
你可以这样做:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.