如果用户在同一网页上出现 3 次,我会使用下面的 JavaScript 将用户重定向到另一个网页,但不幸的是,它不起作用。
var count = Number( localStorage.visitCount );
if(!isNaN(count) {
localStorage.visitCount = 1
} else {
localStorage.visitCount++
}
if( localStorage.visitCount === 3 ) {
window.location.replace('http://stackoverflow.com')
}
重定向不起作用。有人可以告诉我我做错了什么吗?谢谢。
最佳答案
试试这个:
var count = Number( localStorage.visitCount );
if(isNaN(count)) { // <-- you forget bracker here
localStorage.visitCount = 1
} else {
localStorage.visitCount++
}
if( localStorage.visitCount >= 3 ) {
window.location.replace('http://stackoverflow.com')
}
此外,正如 Eric J. 在 this answer 中所说,它看起来像是第一个 if
中的逻辑错误。它应该是 isNaN(count)
,而不是 !isNaN(count)
。解释在他的回答中。
此外,正如 gilly3 在他的帖子中提到的,当 localStorage.visitCount
大于 3 时,您必须处理这种情况。
if( localStorage.visitCount > 3 ) {
// handler for this situation
}
关于javascript - 为什么从同一设备查看 3 次后重定向页面不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23281659/