Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old December 3rd, 2006, 02:01 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,276 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 13 h 24 m 17 sec
Reputation Power: 1795
Post AJAX - generic function plus full sample code.

Yet another AJAX sample code, only this time bundled somewhat
better, at least in my opinion.

The below is consisted of three files:
  1. The AJAX "engine" which is pure JavaScript code and should
    be saved as .js file. (in the sample code: "Shadow_AJAX.js")
  2. Some sort of server side code that AJAX will call. in the sample
    code here it's AJAX_ReadFile.asp
  3. The page that use the AJAX, in the sample code: Test_AJAX.html

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:
  • This version of AJAX will replace new line characters with
    the HTML "<br />" new line tag before sending the response
    back to the client function.
  • This AJAX is using "cache buster" to avoid caching issues.
  • This example will show the contents of text file and "refresh"
    those contents every 5 seconds.

Happy Programming!
Attached Files
File Type: zip Shadow_AJAX.zip (2.0 KB, 365 views)

Last edited by Shadow Wizard : December 3rd, 2006 at 02:35 PM.

Reply With Quote
  #2  
Old January 16th, 2007, 03:58 AM
shem's Avatar
shem shem is offline
Shemzilla
ASP Free Loyal (3000 - 3499 posts)
 
Join Date: Aug 2005
Location: Table View, Cape Town, S.A
Posts: 3,367 shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 17744 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 5 Days 16 m 14 sec
Reputation Power: 531
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.


Reply With Quote
  #3  
Old January 16th, 2007, 04:06 AM
shem's Avatar
shem shem is offline
Shemzilla
ASP Free Loyal (3000 - 3499 posts)
 
Join Date: Aug 2005
Location: Table View, Cape Town, S.A
Posts: 3,367 shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 17744 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 5 Days 16 m 14 sec
Reputation Power: 531
got it working

Reply With Quote
  #4  
Old January 16th, 2007, 04:31 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,276 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 13 h 24 m 17 sec
Reputation Power: 1795
Quote:
Originally Posted by shem
got it working
well done!

Reply With Quote
  #5  
Old January 16th, 2007, 05:40 AM
shem's Avatar
shem shem is offline
Shemzilla
ASP Free Loyal (3000 - 3499 posts)
 
Join Date: Aug 2005
Location: Table View, Cape Town, S.A
Posts: 3,367 shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 17744 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 5 Days 16 m 14 sec
Reputation Power: 531
would it be difficult to change this to work with a form to
update a database?

Shem

Reply With Quote
  #6  
Old January 16th, 2007, 05:45 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,276 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 13 h 24 m 17 sec
Reputation Power: 1795
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.

Reply With Quote
  #7  
Old January 16th, 2007, 05:51 AM
shem's Avatar
shem shem is offline
Shemzilla
ASP Free Loyal (3000 - 3499 posts)
 
Join Date: Aug 2005
Location: Table View, Cape Town, S.A
Posts: 3,367 shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)shem User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 17744 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 5 Days 16 m 14 sec
Reputation Power: 531
which forum would I post this in?

js forum?

Reply With Quote
  #8  
Old January 16th, 2007, 06:21 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,276 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 13 h 24 m 17 sec
Reputation Power: 1795
ASP forum, as the "core" is ASP.
Comments on this post
shem agrees: thanks

Reply With Quote
  #9  
Old March 10th, 2007, 01:12 AM
ssj ssj is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 30 ssj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 14 m 50 sec
Reputation Power: 2
Got it working nicely

Reply With Quote
  #10  
Old October 7th, 2007, 08:50 AM
brcjacks brcjacks is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Location: Atlanta
Posts: 11 brcjacks User rank is Sergeant (500 - 2000 Reputation Level)brcjacks User rank is Sergeant (500 - 2000 Reputation Level)brcjacks User rank is Sergeant (500 - 2000 Reputation Level)brcjacks User rank is Sergeant (500 - 2000 Reputation Level)brcjacks User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 18 m 22 sec
Reputation Power: 0
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?

Reply With Quote
  #11  
Old October 7th, 2007, 09:11 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 45th Plane (27000 - 27499 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,276 Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1Folding Points: 358841 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 6 Days 13 h 24 m 17 sec
Reputation Power: 1795
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.

Reply With Quote