Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么页面不重定向';来自同一设备的3个视图后无法工作?_Javascript_Html_Local Storage_Server Side - Fatal编程技术网

Javascript 为什么页面不重定向';来自同一设备的3个视图后无法工作?

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

如果用户在同一网页上出现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')
}
试试这个:

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
  • 第四次访问发生了什么?如果要在第三次和后续访问时重定向,应使用
    =
  • 我数了3或4个问题:

  • 语法错误-您缺少一个
  • 逻辑错误-丢失
    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