-1

I want to redirect to another page if counter > 5. Can you please help me? This is my code:

<p class="times"> 
     <span id="display">0</span> 
</p>    
      
<script type="text/javascript">
    var count = 0;
    var disp = 0;
    var btn = document.getElementById("btn");
    var disp = document.getElementById("display");
          
    btn.onclick = function () {
        count ++;
        disp.innerHTML = count;
    }
    
</script>
Jacob
  • 41
  • 1
  • 1
  • 11
Ncpro Dz
  • 1
  • 1

3 Answers3

0

You can do that with location.href = "".

<p class="times"> 
     <span id="display">0</span> 
</p>    
      
<script type="text/javascript">
    var count = 0;
    var disp = 0;
    var btn = document.getElementById("btn");
    var disp = document.getElementById("display");
          
    btn.onclick = function () {
        count ++;
        disp.innerHTML = count;
        if (count > 5) {
             location.href = "https://google.com/";
        }
    }
    
</script>
Jacob
  • 41
  • 1
  • 1
  • 11
0
btn.onclick = function () {
    count ++;
    disp.innerHTML = count;
    if(count > 5) {
        //this will change the location of the window object aka. redirect
        window.location.href = 'http://my_addreess.com';
    }
}
MST
  • 121
  • 1
  • 7
0

<p class="times"> 
    
            <span 
            id="display">0</span> 
     </p>
     <button id='btn'>click</button>
        
      
        <script type="text/javascript">
            var count = 0;
            var disp = 0;
            var btn = document.getElementById("btn");
            var disp = document.getElementById("display");
      
            btn.onclick = function () {
                count ++;
                disp.innerHTML = count;
                if (count == 5)window.location.href="https://www.stackoverflow.com"
            }
            
        </script>
DCR
  • 14,737
  • 12
  • 52
  • 115