| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Yet another AJAX sample code, only this time bundled somewhat
better, at least in my opinion. The below is consisted of three files:
Also, attached all files plus text file for your convenience - just unzip the files to ASP web folder and execute Test_AJAX.html from there to see it working. --Shadow_AJAX.js Code:
/*
this function creates and returns XMLHTTP component
if such can be created by the browser.
*/
function GetXmlHTTP() {
//first check for IE:
if (window.ActiveXObject) {
var objXML = 0;
try {
objXML = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(ex) {
try {
objXML = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ex) {
alert("AJAX: your browser does not support proper XMLHTTP");
}
}
return objXML;
}
//maybe Mozilla?
if (window.XMLHttpRequest)
return new XMLHttpRequest();
//unknown browser..
alert("AJAX: unknown browser.");
return 0;
} //end function GetXmlHTTP
/*
this function send request to the given URL via
GET method.
*/
function SendAjaxRequest(strURL, strCallbackFunction) {
//get xmlhttp component:
var objXML = GetXmlHTTP();
//got anything?
if (!objXML)
return false;
//attach local callback function:
objXML.onreadystatechange = function()
{
AjaxPageLoad(objXML, strCallbackFunction);
}
//send request:
objXML.open("GET", strURL, true);
objXML.send(null);
} //end function SendAjaxRequest
/*
this function is called after we got response
from the server.
*/
function AjaxPageLoad(objXML, strCallbackFunction) {
//ready?
if (objXML.readyState != 4)
return false;
//get status:
var status=objXML.status;
//maybe not successful?
if (status != 200) {
alert("AJAX: server status "+status);
return false;
}
//get response text:
var strResponse = objXML.responseText;
//call function
eval(strCallbackFunction+"(\""+ReplaceLines(strResponse.replace(/"/g, "\\\""))+"\");");
return true;
} //end function AjaxPageLoad
function GlobalReplace(strOriginal, strToReplace, strReplaceWith) {
var result=strOriginal;
if ((strOriginal.length == 0)||(strToReplace.length == 0)||(strToReplace == strReplaceWith))
return result;
while (result.indexOf(strToReplace) >= 0)
result = result.replace(strToReplace, strReplaceWith);
return result;
}
function ReplaceLines(s) {
var result = s;
result = GlobalReplace(result, "\n\r", "\n");
result = GlobalReplace(result, "\r\n", "\n");
result = GlobalReplace(result, "\n", "<br />");
return result;
}
--AJAX_ReadLines.asp Code:
<% Option Explicit %>
<%
Const FILE_NAME = "test_ajax.txt"
Dim objFSO, objFile, strFilePath
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
strFilePath = Server.MapPath(FILE_NAME)
If Not(objFSO.FileExists(strFilePath)) Then
Response.Write("ERROR: file does not exist")
Else
Set objFile = objFSO.OpenTextFile(strFilePath)
If objFile.AtEndOfStream Then
Response.Write("ERROR: file is empty")
Else
Response.Write(objFile.ReadAll)
End If
objFile.Close
Set objFile = Nothing
End If
Set objFSO = Nothing
%>
--Test_AJAX.html Code:
<html>
<head>
<script type="text/javascript" src="Shadow_AJAX.js"></script>
<script type="text/javascript">
window.onload = WindowLoad;
function WindowLoad(event) {
SendRequest();
window.setInterval("SendRequest()", 5000);
}
function SendRequest() {
SendAjaxRequest(BustCache( "http://localhost/General/AJAX_ReadFile.asp" ), "AnalyzeResponse")
}
function AnalyzeResponse(strResponse) {
document.getElementById( "FileContents" ).innerHTML = strResponse;
}
function BustCache(strURL) {
var strRandom = "nnn=" + parseInt((Math.random()*999999)+1);
return strURL + ((strURL.indexOf("?") == -1)?"?":"&") + strRandom;
}
</script>
</head>
<body>
The contents of the text file: <div id="FileContents"></div>
</body>
</html>
Remarks:
Happy Programming! ![]() Last edited by Shadow Wizard : December 3rd, 2006 at 02:35 PM. |
|
#2
|
||||
|
||||
|
I unzipped and ran Test_AJAX.html
all i get is an alert error 404?
__________________
Everyone has a photographic memory. Some don't have film. I am a nobody, nobody is perfect, therefore I am perfect.
|
|
#3
|
||||
|
||||
|
got it working
![]() |
|
#4
|
||||
|
||||
|
Quote:
![]() |
|
#5
|
||||
|
||||
|
would it be difficult to change this to work with a form to
update a database? Shem |
|
#6
|
||||
|
||||
|
yes you'll have to change the ASP and HTML files.
it's not going to be simple - if you need help on that start new thread and I'll do my best. ![]() |
|
#7
|
||||
|
||||
|
which forum would I post this in?
js forum? |
|
#8
|
||||
|
||||
|
ASP forum, as the "core" is ASP.
![]() |
|
#9
|
|||
|
|||
|
Got it working nicely
|
|
#10
|
|||
|
|||
|
I don't want to appear to be the village idiot but I tried this in IE 7 and FF 2 and got nothing but:
The contents of the text file: and BTW, why can't I upload an avatar on this board? |
|
#11
|
||||
|
||||
|
how exactly did you try it?
do you have IIS installed? regarding avatar, you have to be member for 30 days and have 30 posts before becoming Contributing Member that can have avatar and other cool stuff. ![]() |