Click here to Skip to main content
15,885,878 members
Articles / Programming Languages / Java

Ajaxion - Standalone AJAX - Part 2 of 2 - C# and Java Example

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
22 Jan 2013CPOL5 min read 55.2K   1.4K   42  
An article about how to keep AJAX simple as it is and get the most out of it.
function Ajaxion(defaultUrl)
{
	// Members
	var request = null;
	var currentEventId = null;
	var allowedEventIdsCsv = null;
	var param = null;

	// Expose Methods
	this.post = post;
	this.postUrl = postUrl;
	this.get = get;
	this.getUrl = getUrl;
	this.parseEventParam = parseEventParam;
	this.showStatus = showStatus;
	this.showStatusImg = showStatusImg;
	this.setEventMonior = setEventMonior;
	this.getParam = getParam;
	this.getResponseString = getResponseString;
	this.getResponseXml = getResponseXml;
	this.getRequest = getRequest;
	this.isEventConsumed = isEventConsumed;
	this.beginEvent = beginEvent;
	this.endEvent = endEvent;

	// Exposed Methods

	function post(eventId, callbackConsumer)
	{
		callUrl("POST", defaultUrl, eventId, callbackConsumer);
	}
	function postUrl(url, eventId, callbackConsumer)
	{
		callUrl("POST", url, eventId, callbackConsumer);
	}

	function get(eventId, callbackConsumer)
	{
		callUrl("GET", defaultUrl, eventId, callbackConsumer);
	}
	function getUrl(url, eventId, callbackConsumer)
	{
		callUrl("GET", url, eventId, callbackConsumer);
	}

	function parseEventParam(eventId)
	{
		return null;
	}
	function getParam()
	{
		if (param != null)
			return param;
		else
			return "";
	}
	function getRequest()
	{
		return request;
	}
	function getResponseString()
	{
		if (request.responseText != null)
			return request.responseText;
		else
			return "";
	}
	function getResponseXml()
	{
		return request.responseXML;
	}
	function setEventMonior(currEventId, currAllowedEventIdsCsv)
	{
		currentEventId = currEventId;
		allowedEventIdsCsv = currAllowedEventIdsCsv;
	}
	function showStatus(elementId, text, backgroundColor)
	{
		window.document.getElementById(elementId).innerHTML = text;
		window.document.getElementById(elementId).style.backgroundColor = backgroundColor;
	}
	function showStatusImg(elementId, imageUrl)
	{
		window.document.getElementById(elementId).src = imageUrl;
	}
	function isEventConsumed()
	{
		return (request.readyState == 4 || request.readyState == "complete");
	}
	function beginEvent(eventId)
	{
		this.setEventMonior(eventId, null);
	}
	function endEvent()
	{
		this.setEventMonior(null, null);
	}
	
	// Internal Methods

	function callUrl(postOrGet, url, eventId, callbackConsumer)
	{
		try
		{
			param = null;
			if (currentEventId == null || isAllowedEventId(eventId))
			{
				request = getXmlHttpRequest();
				if (request != null)
				{
					currentEventId = eventId;
					request.onreadystatechange = callbackConsumer;
					param = this.parseEventParam(eventId);
					var ajaxUrl = initAjaxCallUrl(url, eventId, param);
					var asynchronous = true;
					request.open(postOrGet, ajaxUrl, asynchronous);
					if (postOrGet == "POST")
						request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					request.send(null);
				}
				else alert("Can not create XMLHttpRequest object...");
			}
			else alert("Please wait, " + currentEventId + " is currently runing...");
		}
		catch (ex)
		{
			alert("callUrl exception: " + ex.description);
		}
	}
	function getXmlHttpRequest()
	{
		var xmlHttpReq;
		try
		{
			if (window.XMLHttpRequest != null) // For IE7 and Mozilla browsers
				xmlHttpReq = new XMLHttpRequest();
			else if (window.ActiveXObject) // for IE 5.0 and IE 6.0 browsers
			{
				try
				{
					xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (msEx)
				{
					xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
				}
			}
		}
		catch (ex)
		{
			alert("getXmlHttpRequest exception: " + ex.description);
			xmlHttpReq = null;
		}
		return xmlHttpReq;
	}
	function initAjaxCallUrl(url, eventId, params)
	{
		var urlEventId = "e";
		var urlParam = "p";
		var urlDate = "d";

		var ajaxUrlParams =
			urlEventId + "=" + eventId
			+ (params != null ? "&" + urlParam + "=" + params : "")
			+ "&" + urlDate + "=" + new Date().toUTCString();

		if (url.indexOf("?") == -1)
			return url + "?" + ajaxUrlParams;
		else
			return url + "&" + ajaxUrlParams;
	}
	function isAllowedEventId(eventId)
	{
		if (allowedEventIdsCsv == null || eventId.length > allowedEventIdsCsv.length)
			return false;
		else if (eventId == allowedEventIdsCsv)
			return true;
		else if (allowedEventIdsCsv.indexOf("," + eventId + ",") > 0)
			return true;
		else if (allowedEventIdsCsv.indexOf(eventId + ",") == 0)
			return true;
		else if (allowedEventIdsCsv.indexOf("," + eventId) > 0)
			return true;
		else
			return false;
	}
}

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
Software Developer
New Zealand New Zealand
Coder

Comments and Discussions