jQuery and ASP.NET

January 7, 2009

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() 

        {

            Redirect();

        }

 

        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 =

                "redirecting...";

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

            }

        }        

    </script>

</head>

    <body>

        <div id="cntDisp"/>    

    </body>

</html>



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.

'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Display a Counter before Redirecting Users"
  1. mobile99 said...
    November 12, 2010 2:32 AM

    Nice one !

  2. Anonymous said...
    July 29, 2011 5:40 AM

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

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions