Question

I use this javascript below to redirect a user to another web page if the user comes 3 times on the same webpage, but it's not working unfortunately.

var count = Number( localStorage.visitCount );

if(!isNaN(count) {
   localStorage.visitCount = 1
} else {
   localStorage.visitCount++
}

if( localStorage.visitCount === 3 ) {
   window.location.replace('http://stackoverflow.com')
}

The redirection doens't work. Can someone tell me what I'm doing wrong? Thank you.

Était-ce utile?

La solution

Try this:

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')
}

Also, as Eric J. said in this answer, it looks like as logical mistake in first if. It should be isNaN(count), not !isNaN(count). Explanation is in his answer.

Also, as gilly3 mentioned in his post you have to handle situation, when localStorage.visitCount greater then 3.

if( localStorage.visitCount > 3 ) {
   // handler for this situation
}

Autres conseils

The line

if(!isNaN(count) {

should probably be

if(isNaN(count)) {

You are not initializing count to 1 when it is Not-a-Number. Rather, you are attempting to increment it when it is Not-a-Number.

Also, you are missing a closing parenthesis (my corrected line accounts for that).

So, a couple minor things here.

Firstly, your syntax is off. It looks like you are missing a ")" in your if statement, as well as some missing semi colons.

Secondly, I also see some logic errors.

In your if statement, you want to set the count to 1 if it's not a number, so remove that "!". Otherwise it will do the opposite of what you want.

Also in your second if statement, you want to check if the number is greater than or equal to three, otherwise it will only redirect on the third occasion and not afterwards.

var count = Number(localStorage.visitCount);

if(isNaN(count)) {
   localStorage.visitCount = 1;
} else {
   localStorage.visitCount++;
}

if(localStorage.visitCount >= 3) {
   window.location.replace('http://stackoverflow.com');
}

I count 3 or 4 issues:

  1. Syntax error - You are missing a )
  2. Logic error - Lose the ! before isNaN() (or keep it and use !isFinite())
  3. Type comparison - The === zealots have convinced you to never use ==. In this case, you want == because localStorage variables are always strings and "3" === 3 returns false.
  4. What happens on the 4th visit? If you want to redirect on the 3rd and subsequent visits, you should use >=.

It's working with the following code:

<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>

Thank you guys!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top