'Redirect to another page after mouse counter [duplicate]
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>
Solution 1:[1]
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>
Solution 2:[2]
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';
}
}
Solution 3:[3]
<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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jacob |
| Solution 2 | MST |
| Solution 3 | DCR |
