<input type="button" onclick="req.sendGet('http://localhost/www/js-request.htm',receiveCB);" value="Zobrazit zdrojovy kod">
<div id="xxx"></div>

<script>
function isFunction(obj) {return typeof(obj)=='function';}//) && obj!=null

function escapeHtml(str) {
    var map = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        "\"": "&quot;",
        "'":  "&apos;"
    };
    return str.replace(/[&<>"']/g, function(m) { return map[m]; });
}

function classRequest()
{
var root  = this;
this.req  = null;
this.path = null;
this.fn   = {};
this.construct = function (onsuccess, onerror)
	{
	root.req = new XMLHttpRequest();
	root.req.response = 'responseText';
	//root.setSuccessError(onsuccess, onerror);
//	root.req.onreadystatechange = function()
//		{
//		if (root.req.readyState==4 && root.req.status==200)
//			{root.onSuccessRequestCB(root.req);}
//		else	{root.onTransferCB();}
//		}
	root.req.onload   = root.onSuccessEventCB;
	root.req.onerror  = root.onErrorRequestCB;
	}
this.setSuccessError = function (onsuccess, onerror)
	{
	root.fn.onSuccess = isFunction(onsuccess) ? onsuccess : root.onSuccessCB;
	root.fn.onError   = isFunction(onerror)   ? onerror   : root.onErrorCB;
	}
this.onErrorCB = function (req, path)
	{
	alert("Request error\n"+[path, req.readyState, req.status, req.statusText ? req.statusText : ''].join("\n"));
	};
this.onTransferCB = function ()
	{
	};
this.onSuccessCB = function (data)
	{
	};
this.onErrorRequestCB = function ()		// send / receive error
	{
	alert('error');
	root.fn.onErrorCB(root.req, root.path)
	};
this.onSuccessRequestCB = function (req)	// receive
	{
	alert('success request');
	root.fn.onSuccess(req.responseText);
	};
this.onSuccessEventCB = function (event)	// receive
	{
	alert('success event');
	var data, req;
	data = event && event.target && event.target.response ? event.target.response : (root.req.response ? root.req.response : ''); // zkus ziskat response
	data = data ? data : (event && event.target && event.target.result ? event.target.result : ''); // zkus ziskat result
	root.fn.onSuccess(data);
	};
this.sendGet = function (path, onsuccess, onerror)
	{
	root.setSuccessError(onsuccess, onerror);
	root.path = path;
	root.req.open('GET', path, true);
	root.req.send();
	}
root.construct();
}

function receiveCB(data)
	{
	document.getElementById('xxx').innerHTML = '<pre>'+escapeHtml(data)+'<\/pre>';
	}

var req = new classRequest();
</script>
