Reputation: 63
I am trying to re-direct user to a specific page based on condition. If Timer still running open booknow.html if timer expired open booknowcode.html
Pseudo code
// Set the date we're counting down to
var countDownDate = new Date("June 30, 2021 09:17:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="timer"
document.getElementById("timer").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s " + "Discount 15%";
// If the count down is over, write Expired text
if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
}
//===if timer still running then redirect to dicount page (distance more than zero) Start ===
let link;
if (distance <= 0) {
link = "booknow.html";
} else {
link = "booknowcode.html";
}
document.getElementById("demo").innerHTML = link;
//===if timer still running then redirect to dicount page (distance more than zero) End ===
}, 1000);
<div class="timer-button" id="conditional-button">
<p id="timer"></p>
<a href="booknow.html">
<button class="btn btn-info" type="button" onclick="">Book Now</button>
</a>
</div>
<p id="demo"></p>
So how do I replace booknow.html with booknowcode.html based on the timer condition?
Upvotes: 2
Views: 779
Reputation: 177960
Like this
Version 1
document.getElementById("link").href = "booknowcode.html";
when timer expires// Set the date we're counting down to
var countDownDate = new Date("June 30, 2021 09:17:25").getTime();
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
now = new Date().getTime();
// Find the distance between now and the count down date
distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="timer"
document.getElementById("timer").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s " + "Discount 15%";
// If the count down is over, write Expired text
if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
//===if timer stops, the link changes ===
document.getElementById("link").href = "booknowcode.html";
}
}, 1000);
<div class="timer-button" id="conditional-button">
<p id="timer"></p>
<a href="booknow.html" class="btn btn-info" id="link">Book Now</button>
</div>
Version 2
document.getElementById("demo").innerHTML = link;
to location = link
when running// Set the date we're counting down to
var countDownDate = new Date("June 30, 2021 09:17:25").getTime();
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
now = new Date().getTime();
// Find the distance between now and the count down date
distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="timer"
document.getElementById("timer").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s " + "Discount 15%";
// If the count down is over, write Expired text
if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
}
//===if timer still running then redirect to dicount page (distance more than zero) Start ===
let link;
}, 1000);
document.getElementById("but").addEventListener("click", function() {
if (distance <= 0) {
link = "booknow.html";
} else {
link = "booknowcode.html";
}
document.getElementById("demo").innerHTML = link;
// change this to
// location = link;
// when running
//===if timer still running then redirect to dicount page (distance more than zero) End ===
})
<div class="timer-button" id="conditional-button">
<p id="timer"></p>
<button class="btn btn-info" id="but" type="button">Book Now</button>
</div>
<p id="demo"></p>
Upvotes: 1
Reputation: 89264
You should set the href
instead of the innerHTML
.
document.querySelector("#conditional-button a").href = link;
Upvotes: 3
Reputation: 23654
Just apply your new link to the <a>
(which I gave a data-attribute to make our replacement exact)
if (distance <= 0) {
// ....
document.querySelector('a[data-rel=book-link]').setAttribute('href', link)
I wasn't sure why you had a button inside of an anchor tag, so I removed the button and applied the css to the a
and it should look the same.
// Set the date we're counting down to
//var countDownDate = new Date("June 30, 2021 09:17:25").getTime();
countDownDate = new Date().getTime() + 10000;
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="timer"
document.getElementById("timer").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s " + "Discount 15%";
let link;
// If the count down is over, write Expired text
if (distance <= 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
link = "booknow.html";
document.querySelector('a[data-rel=book-link]').setAttribute('href', link)
//===if timer still running then redirect to dicount page (distance more than zero) Start ===
} else {
link = "booknowcode.html";
}
document.getElementById("demo").innerHTML = link;
//===if timer still running then redirect to dicount page (distance more than zero) End ===
}, 1000);
<div class="timer-button" id="conditional-button">
<p id="timer"></p>
<a data-rel='book-link' href="booknow.html" class="btn btn-info">Book Now</a>
</div>
<p id="demo"></p>
Upvotes: 0