Javascript 为什么页面不重定向';来自同一设备的3个视图后无法工作?
如果用户在同一网页上出现3次,我会使用下面的javascript将用户重定向到另一个网页,但不幸的是,它无法工作Javascript 为什么页面不重定向';来自同一设备的3个视图后无法工作?,javascript,html,local-storage,server-side,Javascript,Html,Local Storage,Server Side,如果用户在同一网页上出现3次,我会使用下面的javascript将用户重定向到另一个网页,但不幸的是,它无法工作 var count = Number( localStorage.visitCount ); if(!isNaN(count) { localStorage.visitCount = 1 } else { localStorage.visitCount++ } if( localStorage.visitCount === 3 ) { window.locati
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')
}
试试这个:
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')
}
线路
if(!isNaN(count) {
应该是
if(isNaN(count)) {
当计数不是数字时,您没有将其初始化为1。相反,当它不是一个数字时,您试图增加它
另外,您缺少一个右括号(我更正的行说明了这一点)。行
if(!isNaN(count) {
应该是
if(isNaN(count)) {
当计数不是数字时,您没有将其初始化为1。相反,当它不是一个数字时,您试图增加它
此外,您还缺少一个右括号(我更正的行说明了这一点)。因此,这里有几个小问题 首先,你的语法不正确。看起来您的if语句中缺少了一个“)”以及一些缺少的分号 其次,我也看到了一些逻辑错误 在if语句中,如果不是数字,则要将计数设置为1,因此请删除该“!”。否则,它将与你想要的相反 同样在第二个if语句中,您希望检查数字是否大于或等于3,否则它只会在第三次重定向,而不会在第三次之后重定向
var count = Number(localStorage.visitCount);
if(isNaN(count)) {
localStorage.visitCount = 1;
} else {
localStorage.visitCount++;
}
if(localStorage.visitCount >= 3) {
window.location.replace('http://stackoverflow.com');
}
这里有几件小事 首先,你的语法不正确。看起来您的if语句中缺少了一个“)”以及一些缺少的分号 其次,我也看到了一些逻辑错误 在if语句中,如果不是数字,则要将计数设置为1,因此请删除该“!”。否则,它将与你想要的相反 同样在第二个if语句中,您希望检查数字是否大于或等于3,否则它只会在第三次重定向,而不会在第三次之后重定向
var count = Number(localStorage.visitCount);
if(isNaN(count)) {
localStorage.visitCount = 1;
} else {
localStorage.visitCount++;
}
if(localStorage.visitCount >= 3) {
window.location.replace('http://stackoverflow.com');
}
我数了3或4个问题:
)
在isNaN()
之前使用code>(或者保留它并使用!isFinite()
)
=
。在这种情况下,您需要=
,因为localStorage
变量始终是字符串,“3”==3
返回false
=
)
在isNaN()
之前使用code>(或者保留它并使用!isFinite()
)
=
。在这种情况下,您需要=
,因为localStorage
变量始终是字符串,“3”==3
返回false
=
它正在使用以下代码:
<script>
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://www.website.com')
}
if( localStorage.visitCount >= 3 ) {
window.location.replace('http://www.website.com')
}
</script>
var count=Number(localStorage.visitCount);
如果(isNaN(计数)){/=3){
window.location.replace('http://www.website.com')
}
谢谢大家!它正在使用以下代码:
<script>
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://www.website.com')
}
if( localStorage.visitCount >= 3 ) {
window.location.replace('http://www.website.com')
}
</script>
var count=Number(localStorage.visitCount);
如果(isNaN(计数)){/=3){
window.location.replace('http://www.website.com')
}
谢谢大家!定义“不工作”。它只是不重定向吗?是否有一些错误消息?定义“不工作”。它只是不重定向吗?是否有一些错误消息?不幸的是,重定向仍然无法与以下代码一起工作:
var count=Number(localStorage.visitCount);if(isNaN(count)){/@Jan再试一次。我删除了!
中的如果语句没有,它仍然没有重定向:(你必须在你的问题中澄清它,正如@EricJ在第一条评论中注意到的那样。不幸的是,重定向仍然不能与以下代码一起工作:var count=Number(localStorage.visitCount);if(isNaN(count)){/@Jan请再试一次。我删除了中的!
如果语句,它仍然没有重定向:(你必须在你的问题中澄清它,正如@EricJ在第一条评论中注意到的那样。如果的话,你不需要2。使用本地存储.visitCount>=3
你不需要2如果的话。使用本地存储.visitCount>=3