JakeGriffin
JakeGriffin

Reputation: 223

Javascript countdown and redirect

I have a javascript script that runs a countdown and then redirects to a different link.

Since I now have all my sites running off the same code using a static domain, I would like to know if it is possible to edit the script to make it redirect to a different link depending on the "span" tag it is wrapped around.

(I also butchered this code, so if anyone has a tidier way of doing it and wants to clean it up, that'd be great.)

Javascript

var time_left = 3;
var cinterval;
 
function time_dec(){
    time_left--;
    document.getElementById('countdown').innerHTML = time_left;
    if(time_left == 1){
        var originalstring = document.getElementById('countdown2').innerHTML;
        var newstring = originalstring.replace('seconds','second');
        document.getElementById('countdown2').innerHTML = newstring;
        window.location.replace("http://mydomainhere.com");
        clearInterval(cinterval);
    }
}
 
cinterval = setInterval('time_dec()', 1000);

HTML

<p>Redirecting you <span id="countdown2">in <span id="countdown">3</span> seconds</span>.</p>

If anyone could help me out that would be much appreciated.

Upvotes: 4

Views: 5643

Answers (1)

christopher
christopher

Reputation: 27346

window.location.replace("http://mydomainhere.com");

This is really the only important line. Instead of using a span tag, I'd just stick a hidden element on the page.

<input id="url" type="hidden" value="http://www.adomain.com"/>

Then you grab the value:

var url = document.getElementById("url").getAttribute("value");

window.location.replace(url);

Upvotes: 2

Related Questions