All pastes #478258 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-05-09 01:55 UTC
rendered paste body
var AJAX = {
	
	XMLHttpRequest: function()
	{
		var req = false;
		
		if(window.XMLHttpRequest && !(window.ActiveXObject))
		{
			try
			{
				req = new XMLHttpRequest();
			}
			catch(e)
			{
				req = false;
			}

		}
		else if(window.ActiveXObject)
		{
			try
			{
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					req = false;
				}
			}
		}
		
		return(req);
	},
	
	Response: function(xmlobject)
	{
		this.status = xmlobject.status;
		this.statusText = xmlobject.statusText;
		this.responseText = xmlobject.responseText;
		this.responseXML = xmlobject.responseXML;
	},
	
	Request: function(url, options)
	{
		if (!isset(url))
		{
			return(false);
		}
		
		this.url = url;
		this.method = (isset(options.method)) ? options.method : 'GET';
		this.asynchronous = (isset(options.asynchronous) && typeof(options.asynchronous) == "boolean") ? options.asynchronous : true;
		this.contentType = (isset(options.contentType)) ? options.contentType : 'application/x-www-form-urlencoded';
		this.parameters = (isset(options.parameters)) ? options.parameters : '';
		
		if (typeof(this.parameters.join) != "Undefined")
		{
			this.parameters = this.parameters.join("&");
		}
		
		this.encoding = (isset(options.encoding)) ? options.encoding : 'iso-8859-1';
		
		this.callbacks = {};
		
		this.dispatch = function(event, parameter)
		{
			if (isset(this.callbacks[event]))
			{
				if (isset(parameter))
				{
					this.callbacks[event](parameter);
				}
				else
				{
					this.callbacks[event]();
				}
			}
		}
		
		this.handler = function()
		{
			switch(this.object.readyState)
			{
				case 3:
					this.dispatch('onLoading');
					break;
				
				case 4:
					this.dispatch('onComplete', new AJAX.Response(this.object));
					break;
			}
		}
		
		if (isset(options.onComplete))
		{
			this.callbacks['onComplete'] = options.onComplete;
		}
		
		if (isset(options.onFailure))
		{
			this.callbacks['onFailure'] = options.onFailure;
		}
		
		this.object = new AJAX.XMLHttpRequest();
		
		if (!this.object)
		{
			this.dispatch('onFailure');
			return false;
		}

		this.object.onreadystatechange = this.handler.bind(this);
		
		if (this.method == "GET")
		{
			this.object.open(this.method, this.url + "?" + this.parameters, this.asynchronous);

		}
		else
		{
			this.object.open(this.method, this.url, this.asynchronous);
			
		}
		
		this.object.setRequestHeader("Content-Encoding", this.encoding);
		this.object.setRequestHeader("Content-Type", this.contentType);
		
		if (this.method == "GET")
		{
			this.object.send("");
		}
		else
		{
			this.object.send(this.parameters);
		}
		
		if (!this.asynchronous)
		{
			this.response = new AJAX.Response(this.object);
		}
	}
	
}

function isset(check)
{
	return(typeof(check) != "undefined");
}

Function.prototype.bind = function()
{
	var __method = this;
	args = $A(arguments);
	object = args.shift();
	
	return function()
	{
		return __method.apply(object, args.concat($A(arguments)));
	}
}

var $A = Array.from = function(iterable)
{
	if (!iterable) return [];
	if (iterable.toArray)
	{
		return iterable.toArray();
	}
	else
	{
		var results = [];
		for (var i = 0, length = iterable.length; i < length; i++)
		{
			results.push(iterable[i]);
		}
		return results;
	}
}