Ok, so here are the steps that you need to take:
1) Remove the meta refresh in that file.
2) You need to make a new .asp file that can create an xml document of all of the new messages from a given point (example: getMessages.asp). So, if you have in your database say, a messageID column; have the new file be able to accept a URL parameter of messageID and then run a query that gets all messages that were created after that messageID, like this:
Code:
DIM sql
sql = 'SELECT * FROM messages WHERE messageID > ' + Request.QueryString("messageID")
(forgive me if that's not exactly correct, I haven't actually coded in asp in years.)
Real quick, you'll have to change the content type of this file to it to xml text. I believe its
Code:
Response.contentType = "text/xml"
Put that at the top of the new file.
This new file should basically just have output that looks something along the lines of:
Code:
<messages>
<message>
<username>Person 1</username>
<messageText>Hey man, whats good</messageText>
</message>
<message>
<username>Person 2</username>
<messageText>Yo whats up</messageText>
</message>
</messages>
You'll just have to loop out the records from the database into that format.
3) We're going to use an AJAX library to get the data. Honestly, you don't want to even be bothered with the inner workings of XMLHttpRequest and such. If you really wanted to, you could make your own Ajax class, but its really a big waste of time when nice people at yahoo have done that for us, while also figuring out all the stupid cross-browser nuances. The yui library provides a simple interface to ajax calls (and also a nice way of interacting with browser events). I'll show you what I mean in #5 when I give you a code example.
4) We're going to need a hook to an element to append the new data to it. I recommend that <p class="style1"> tag. Make it <p
id="messageOutput" class="style1"> so we can get a reference to it by document.getElementById.
5) A basic script to perform the needed functionality would be the following. You can download the files from yahoo, or you could just link to them on their web server like I usually do (the <script> tags which are at the top). This should all be in the <head> of the document btw. Just get rid of the script you have right now and replace it with this. Unfortunately I have to go right now so I cant finish the script that I gave you just yet, but start with what is here in this post and see if you can get the alerts to pop up with response data from the ajax request. Sorry, but I didn't have time to actually test this just yet, but give it a whirl.
Code:
<!-- Include the YUI library classes we need -->
<script src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js"></script>
<script src="http://yui.yahooapis.com/2.7.0/build/event/event-min.js"></script>
<script src="http://yui.yahooapis.com/2.7.0/build/connection/connection-min.js"></script>
<script>
var Event = YAHOO.util.Event;
var MessageUpdater = new function() {
this.url = "getMessages.asp";
this.lastMessageID = 0;
this.updateTimer = null;
// Initialize the MessageUpdater
this.init = function() {
// Update messages every 2 seconds
this.updateTimer = YAHOO.lang.later( 2000, this, this.updateMessages, null, true ); // arguments are: 2 secs, the 'this' scope, run this.updateMessages function, no extra arguments, runs periodically
}
// Update messages (runs periodically every 2 seconds)
this.updateMessages = function() {
var thisObj = this;
var callbacks = {
success: function() { thisObj.handleSuccess( o ); }, // Call MessageUpdater.handleSucces();
failure: function() { thisObj.handleFailure( o ); } // Call MessageUpdater.handleFailure();
};
var postData = 'messageID=' + this.lastMessageID;
// Make the request
var request = YAHOO.util.Connect.asyncRequest('POST', this.url, callbacks, postData);
}
// Function to handle the success of message retrieval
var handleSuccess = function( o ) {
var str = "";
if( o.responseText !== undefined ){
str = "Transaction id: " + o.tId + "\n";
str += "HTTP status: " + o.status + "\n";
str += "Status code message: " + o.statusText + "\n";
str += "HTTP headers: <ul>" + o.getAllResponseHeaders + "\n";
str += "Server response: " + o.responseText + "\n";
alert( str );
} else {
alert( 'No response text, bad ajax call?' );
}
}
// Function to handle the failure of message retrieval
this.handleFailure = function( o ) {
var str = "";
if( o.responseText !== undefined ) {
str = "Transaction id: " + o.tId + "\n";
str += "HTTP status: " + o.status + "\n";
str += "Status code message: " + o.statusText + "\n";
alert( str );
}
}
}
// When the window finishes loading, start the message updater
Event.addListener( window, 'load', MessageUpdater.init, MessageUpdater, true );
</script>