HTML,打开链接点击页面上的任意位置

我有我想使用的可点击背景代码,但是当我点击背景时,它总是打开的。如何让这段代码每天运行一个,但以最简单的方式可能......使用 cookie 或其他东西。我真的需要这方面的帮助。谢谢!

<body onclick="location.href='test.html';">


慕虎7371278
浏览 207回答 2
2回答

繁花不似锦

如果您想限制用户每天只能打开一次链接。你可以这样做:<body onclick="openLink()"><script>function openLink() {&nbsp; var today = new Date();&nbsp; var dd = String(today.getDate()).padStart(2, '0');&nbsp; var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!&nbsp; var yyyy = today.getFullYear();&nbsp; today = mm + '/' + dd + '/' + yyyy;&nbsp; // As date object returns time as well. Which we dont need. So we remove that.&nbsp; if(localStorage.getItem('date') == today) {&nbsp; &nbsp; alert('come back tomorrow');&nbsp; } else {&nbsp; &nbsp; localStorage.setItem('date', today);&nbsp; &nbsp; location.href='test.html';&nbsp; }}</script>

青春有我

您可以使用localStorage.<script>&nbsp; &nbsp; function onBodyClick() {&nbsp; &nbsp; &nbsp; &nbsp; var lastOpened = localStorage.getItem('body-opened'); // You can use another identifier instead of 'body-opened'&nbsp; &nbsp; &nbsp; &nbsp; if (lastOpened && new Date(lastOpened).toDateString() === new Date().toDateString()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localStorage.setItem('body-opened', new Date().toDateString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.location.href = 'test.htm';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }</script><body onclick="onBodyClick()"></body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript