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