, a code gallery for creating, saving and sharing all kinds of code snippets.

Redirect with a Timer — Javascript

Posted On: Saturday, April 25, 2015 at 11:21 PM

You can use JavaScript window.location to redirect a visitor to a required page. You may have seen this feature used by sites with full page ads, or to redirect visitors to the site's new domain name. You can also add a timer to the script that will allow the page to redirect automatically after x seconds.

<script type="text/javascript">
var count = 6;
var redirect = "";

function countDown(){
var timer = document.getElementById("timer");
if(count > 0){
timer.innerHTML = "You'll be redirected to our new site in "+count+" seconds.";
setTimeout("countDown()", 1000);
window.location.href = redirect;

<p>Our webpage has moved. Please update your bookmarks for our new site.</p>

<span id="timer">
<script type="text/javascript">countDown();</script>

You need to be logged in to comment! x


Joined on Sunday, May 11, 2014
Contributed 2 Snippets

Get Social and Share