Click here to Skip to main content
15,891,864 members
Articles / Web Development / HTML

AJAX Explained

Rate me:
Please Sign up or sign in to vote.
4.35/5 (42 votes)
17 May 2006CPOL4 min read 155.1K   480   106  
An introduction to AJAX for ASP.NET applications.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="PageServerTime.aspx.vb" Inherits="AjaxApp.PageServerTime"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title>Ajax Example</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		
		<style> 
			body, input, div {font-family: arial; font-size: 11px;} 
		</style> 

		<!-- Insert JavaScript Here --> 
		<script> 
			var xmlHttp; 
			var requestURL = 'getServerTime.aspx'; 
			//var requestURL = 'http://localhost:100/CMSNew/PriorityCompany_NewGrid.aspx'; 
			var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
			var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
			var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
			//netscape, safari, mozilla behave the same??? 
			var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 

			function showServerTime()
			{ 				
				//Append the name to search for to the requestURL 
				var url = requestURL + '?q=' + Math.random(); 
		            
				//Create the xmlHttp object to use in the request 
				//stateChangeHandler will fire when the state has changed, i.e. data is received back 
				// This is non-blocking (asynchronous) 
				xmlHttp = GetXmlHttpObject(stateChangeHandler); 
		            
				//Send the xmlHttp get to the specified url 
				xmlHttp_Get(xmlHttp, url); 
				
			} 

			//stateChangeHandler will fire when the state has changed, i.e. data is received back 
			// This is non-blocking (asynchronous) 
			function stateChangeHandler() 
			{ 
				//readyState of 4 or 'complete' represents that data has been returned 
				if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){ 
					//Gather the results from the callback 
					var str = xmlHttp.responseText; 
					
					//Populate the innerHTML of the div with the results 
					document.getElementById('serverTime').innerHTML = 'The time on the server is <b>' + str; 
				} 
			} 

			// XMLHttp send GET request 
			function xmlHttp_Get(xmlhttp, url) { 
				xmlhttp.open('GET', url, true); 
				xmlhttp.send(null); 
			} 

			function GetXmlHttpObject(handler) { 
				var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

				//Depending on the browser, try to create the xmlHttp object 
				if (is_ie){ 
					//The object to create depends on version of IE 
					//If it isn't ie5, then default to the Msxml2.XMLHTTP object 
					var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
		             
					//Attempt to create the object 
					try{ 
						objXmlHttp = new ActiveXObject(strObjName); 
						objXmlHttp.onreadystatechange = handler; 
					} 
					catch(e){ 
					//Object creation errored 
						alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
						return; 
					} 
				} 
				else if (is_opera){ 
					//Opera has some issues with xmlHttp object functionality 
					alert('Opera detected. The page may not behave as expected.'); 
					return; 
				} 
				else{ 
					// Mozilla | Netscape | Safari 
					objXmlHttp = new XMLHttpRequest(); 
					objXmlHttp.onload = handler; 
					objXmlHttp.onerror = handler; 
				} 
		         
				//Return the instantiated object 
				return objXmlHttp; 
			} 
			
		</script> 

	</head>
	<body> 
		<form name="frmServerTime" id="frmServerTime"> 
			<table border="0" cellpadding="4" cellspacing="0" id="Table1"> 
				<tr>					
					<td><input type="button" name="btnTime" value="Show Server Time" id="btnTime" onclick="showServerTime();"></td> 
				</tr> 
				<tr>					
					<td> 
						<div id="serverTime"></div> 
					</td> 
				</tr> 
			</table> 
		</form>  
	</body> 
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions