I am trying to have a logout page where is displays a messages and then redirects to the login page. This is in ASP.net 2.0.
I have this in my Page_Load:
ClientScript.RegisterStartupScript(typeof(Page), 'pageredirect', JavascriptRedirect() );
This is my redirect function:
private string JavascriptRedirect() { StringBuilder sb = new StringBuilder(); sb.Append('<script type=\'text/javascript\' language=\'javascript\'>'); sb.Append('var x = 5;'); sb.Append('var y = 1;'); sb.Append('function startClock(){'); sb.Append('x = x-y;'); sb.Append('t=setTimeout(\'startClock()\', 1000);'); sb.Append('if(x==0){'); sb.Append('window.location='login.aspx';'); sb.Append('clearTimeout(t);'); sb.Append(' }'); sb.Append(' }'); sb.Append('startClock();'); sb.Append('</script>'); return sb.ToString(); }
When I test there is no javascript in my resulting logout page. Anyone have an idea on what is happening. Is Page_Load to late to register this?
By the way: You don’t need Javascript to redirect the browser to a page after a certain amount of time. Just use a plain HTML meta Tag in your
<HEAD>section.The number stands for the time in seconds, the URL for the target.