Refresh Page On Timer

admin

Administrator
Staff member
Is there any way to get an asp page to refresh periodically (say every 5 seconds) on a timer?a meta refresh with no URL can be used to refresh a page periodically.
<meta http-equiv="refresh" content="5">where 5 could be any number of seconds. remember that this tag belongs in the <head>.

also, if you would prefer, you could do this the ASP way, by sending that info through the HTTP headers, which is a little cleaner.<% Response.AddHeader "Refresh","5" %>Just what I was looking for. Thanks!!!no prob. :)I don't suppose there is any way to get the refresh to happen less visibly?I don't suppose there is any way to get the refresh to happen less visibly?

Maybe with Ajax XMLHttpRequest, does the whole page really need to be refreshed ?No, I am writing a chat room in asp because I don't know ajax. I have it all functional except for the annoyingly visible refresh. Can I implement the ajax http..... fairly easily?

Right now the page that is refreshing is in an iframe and just displays the text of the chat. I would be VERY HAPPY to take the iframe out and replace it with a portion of the page that would refresh using ajax.Using Ajax would mean the user would have to have javascript enabled, changing the frame (iframe) to a DIV element and setting the data returned from the XMLHttpRequest to that DIV and of course, the form post will need changing to the javascript function.

<!-- m --><a class="postlink" href="http://www.quirksmode.org/js/xmlhttp.htmlHere">http://www.quirksmode.org/js/xmlhttp.htmlHere</a><!-- m --> is what I have now:

Parent page:

<td valign="top" rowspan="3">
<iframe name="chatRoom"
<%If sChatMtg = "chat" Then%>
src=http://www.htmlforums.com/archive/index.php/"chat_text.asp?chat_mtg=<%=sChatMtg%>&this_chat=<%=lThisChat%>"
<%ElseIf sChatMtg = "mtg" Then%>
src=http://www.htmlforums.com/archive/index.php/"mtg_text.asp?chat_mtg=<%=sChatMtg%>&this_mtg=<%=lThisChat%>"
<%End If%>
style="width:650px;height:450px;border:medium inset #009933;"></iframe>
</td>


iframe page (I will spare you the server-size stuff--just populates an array from the
chat data in a sql server db):

<body style="background-color:#ffffff;">
<%For i = 0 To UBound(ThisChat, 2) - 1%>
<div style="padding:5px;">
<span style="font-size:0.85em;font-weight:bold;color:<%=ThisChat(5, i)%>;">
At <%=ThisChat(0, i)%> <%=ThisChat(1, i)%>
(<%=ThisChat(2, i)%>) of <%=ThisChat(3, i)%> wrote:
</span>
<br />
<span style="font-size:0.95em;"><%=ThisChat(4, i)%></span>
<br />
<br />
</div>
<%Next%>
</body>


I understand that I change the iframe to a div in the parent page. What I don't understand is how/what to put in the div. Thanks for anything you can do...Difficult to say without seeing how the chat_text.asp page is called, but I'll give you this example to try out, I've replaced the form submit (which I assume you're using) with a button click to call the function, I'm also assuming the chat_text.asp page is formatted server-side (which I guess is has to be).


<script>
function Requestchat(url) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP');
return false;
}
http_request.onreadystatechange = alertContent;
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContent() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
result = http_request.responseText;
/// NEW Contents DIV
document.getElementById('chatcontents').innerHTML=result;
///
}
}
}
</script>

<!-- using this for an example -->
<button onclick="Requestchat('<%If sChatMtg = "chat" Then%>chat_text.asp?chat_mtg=<%=sChatMtg%>&this_chat=<%=lThisChat%>
<%ElseIf sChatMtg = "mtg" Then%>mtg_text.asp?chat_mtg=<%=sChatMtg%>&this_mtg=<%=lThisChat%>
<%End If%>')">test</button>

<!-- // end -->
<!-- contents box/div -->
<div id="chatcontents"></div>


hopefully this will help you see how the XMLHttpRequest works, but let me know more about the submitting of the data and I'll see if I can help more.
 
Back
Top