Display a Counter before Redirecting Users

A lot of sites display a counter before redirecting a user. Let us see how to create this counter using JavaScript. The solution has been tested in IE7 and Firefox.

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Redirect Users</title>

    <script type="text/javascript" language="javascript">

        window.onload = function() 





        var cnt = 10;

        function Redirect()


            if (cnt > 0){

                document.getElementById("cntDisp").innerHTML = 

                cnt + " seconds left..";                

                cnt = cnt - 1;

                setTimeout("Redirect()", 1000);


            else {

                document.getElementById("cntDisp").innerHTML =


                window.open("http://www.dotnetcurry.com/", "_self");






        <div id="cntDisp"/>    



The code displays a counter for 10 seconds and then redirects the user to the desired url. The same code cab be used on an ASP.NET Page.

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.


mobile99 said...

Nice one !

Anonymous said...

Finally something understandable about redirects with timer. :) Thanks, Suprotim Agarwal!