Wednesday, August 08, 2007

How to do auto page refresh in ASPX

How to do auto page refresh in ASPX

If you have a requirement something like auto redirect page to login page or auto refresh the current window within 10 sec or auto close window after 10 sec then you can write some lines of code in java script to achieve this

There are multiple ways to auto refresh/auto close browser, some very simple ways are below with examples.

1) Auto page refresh à you can write below code inside <head> tag on top of your ASPX page
<head runat="server">
<meta http-equiv="Refresh" content="10;url=''">
</head>
Note: Content takes two parameter 1) <number of second>
2) <URL>

2) Auto page redirect à you can write below code inside <head> tag on top of your ASPX page

<head runat="server">
<meta http-equiv="Refresh" content="10;url='http://localhost/Home/Login.aspx'">
</head>
Note:
http-equiv="Refresh" à This tag will do page auto refresh
content="<number of second>,<URL>" à action when page refresh

3) Auto new page open à If you want to open new window after 10 sec then you can create a java script function say "OpenNewWindow" and use this function like below

<head runat="server">
<meta http-equiv="Refresh" content="10;url='javascript: OpenNewWindow('http://localhost/Home/Login.aspx',650,1000)">
</head>

Note: content="<number of second>,<URL>" , 650 =height 1000= width.

function OpenNewWindow(url, h, w)
{
window.open(url,"","width=" + w + ",height=" + h + ",scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,
directories=no,status=yes,left=5,top=5");
}

4) Auto page close à if you want to close the browser after 10 sec then write below code on top of your ASPX page
<script language="JavaScript">
var timestamp= 10;
{
window.setTimeout("window.close()",timestamp*1000);
window.opener = top;
}
</script>
//OR
<script language="JavaScript">
window.setTimeout("window.close()",10*1000);
</script>

No comments: