XMLHttpRequest Factory
/* XMlHttpRequest Factory */
function XMlHttpRequestFactory()
{
var xmlHttp = null;
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
//alert(xmlHttp + '[Status = ' + xmlHttp.readyState + ']' );
}
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//alert(xmlHttp + '[Status = ' + xmlHttp.readyState + ']');
}
catch(e)
{
}
}
return xmlHttp;
}
Send Asynchronous Request to Server
/* Send Asynchronous Request to Server */
/* URL : reqested URL; callBackMethod: method to be called when the response is ready */
function SendAsynchronousRequest(xmlHttp, URL, callBackMethod, httpVerb, contentType)
{
if(xmlHttp != null)
{
xmlHttp.open(httpVerb,GenerateUniqueURL(URL),true);
xmlHttp.onreadystatechange = callBackMethod;
xmlHttp.setRequestHeader('Content-type',contentType);
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.send(null);
}
else
{
throw new Error('Can Not send a Request from the browser, no XMlHttpRequest object found');
}
}
Toggeling Request Status
/* Request Status */
/* XMlHttpRequest Object */
function RequestStatus(xmlHttp)
{
var status = false;
if(xmlHttp.readyState == 0)
{
//alert("Request UNSENT");
}
else if(xmlHttp.readyState == 1)
{
//alert("Request OPENED");
}
else if(xmlHttp.readyState == 2)
{
//alert("Request HEADERS_RECEIVED");
}
else if(xmlHttp.readyState == 3)
{
//alert("Request LOADING");
}
else if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
alert("Response DONE SUCCESSFULLY");
status = true;
}
return status;
}
Hide UpdateProgress Image
* Show Update Progress */
/* xmlHttp : XMlHttpRequest Object; imageID : Image to show/hide */
function ShowUpdateProgress(xmlHttp, imageID)
{
var loader = document.getElementById(imageID);
if(loader != null)
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
loader.style.visibility = "hidden";
}
}
}
Generate Unique URL to avoid caching problems :
/* Generate Unique URL */
/* URL : Uniform Resource Locator */
function GenerateUniqueURL(URL)
{
var sep = (-1 < URL.indexOf("?")) ? "&" : "?";
URL = URL + sep + "__=" + encodeURIComponent((new Date()).getTime());
return URL;
}
Add Client Side PageLoad
/* AddClientPageLoad */
/* scriptManager : SYS.Application */
/* loadFuntion : function to be called at page load */
function AddClientPageLoad(scriptManager, loadFuntion)
{
scriptManager.add_load(loadFuntion);
}
Add Client Side PageLoad
/* AddClientPageUnLoad */
/* scriptManager : SYS.Application */
/* UnloadFuntion : function to be called at page load */
function AddClientPageUnLoad(scriptManager, UnloadFuntion)
{
scriptManager.add_load(UnloadFuntion);
}
Enjoy it!