perkiset

This is a class I wrote as a browser-agnostic

Ajax

  requestor. Note that it is not proper

AJAX

  because it does not REQUIRE xml as either input or output.

Usage:

ajax

 1 = new

ajax

 Requestor()

ajax

 1.url = '/handlingPage.

php

 ';

ajax

 1.postParam('aPostParameter', 'theValue');

ajax

 1.onSuccess = handle

Ajax

 ;

ajax

 1.execute();

function handle

Ajax

 (sender)
{
alert(sender.lastResponse);
}

Essentially, you create one, give it a URL it will be going to, optionally add parameters in the POST content of the request, set a function to handle the response, and execute it. Note that it is in the postParameters where you would place XML, or put many post parameters or whatever. The handler is sent a single parameter - which I most often call "sender" - access the lastResponse property to see what you got back. It can be HTML, XML, anything at all - it's up to you.

I often do stuff like this:

ajax

 1.postParam('request', 'savestuff');

ajax

 1.postParam('firstname', document.getElementById('fname').value);

and such... where I push values directly from the HTML document into the requestor without the structure or weight of XML at all. I often just ship back straight up HTML that is already formatted and ready to go as well:

function handle

Ajax

 (sender) { document.getElementById('answerArea').innerHTML = sender.lastResponse; }

The request at the server looks identical to a normal page request - you don't need to do anything wierd or different at all. Here is the code for my class:


// ----------------------------------------------------------- //
//                     

ajax

 Requestor                        //
// ----------------------------------------------------------- //
function

ajax

 Requestor() { this.clearAll(); }

ajax

 Requestor.prototype.__defaultError = function(sender)
{
var tempStr = "

ajax

 Requestor Error: " +
                  "status: " + this.requestor.status + " " +
          "headers: " + this.requestor.getAllResponseHeaders();
alert(tempStr);
}

ajax

 Requestor.prototype.__defaultSuccess = function(sender)
{
alert("

ajax

 Requestor successfully returned from a request - but there is no handler assigned to receive it");
}

ajax

 Requestor.prototype.__decodeString = function(inputStr)
{
var decoded = unescape(inputStr);
decoded = decoded.replace(/\%2F/g, "/");
decoded = decoded.replace(/\%3F/g, "?");
decoded = decoded.replace(/\%3D/g, "=");
decoded = decoded.replace(/\%26/g, "&");
decoded = decoded.replace(/\%40/g, "@");
return decoded;
}

ajax

 Requestor.prototype.__encodeString = function(inputStr)
{
var encoded = escape(inputStr);
encoded = encoded.replace(///g,"%2F");
encoded = encoded.replace(/?/g,"%3F");
encoded = encoded.replace(/=/g,"%3D");
encoded = encoded.replace(/&/g,"%26");
encoded = encoded.replace(/@/g,"%40");
return encoded;
}

ajax

 Requestor.prototype.__getParams = function()
{
if (this.getNames.length == 0) { return ""; }
var out = (this.url.indexOf('?') == -1) ? '?' : '&';
for (var i=0; i<this.getNames.length; i++)
{
out += this.getNames<> + '=' + this.getValues<>;
if (i < (this.getNames.length - 1)) { out += '&'; }
}
return out;
}

ajax

 Requestor.prototype.__getRequestor = function()
{
if ((this.requestor != null) && (!this.reqIsIE)) { return true; }

try {
this.requestor = new XMLHttpRequest();
this.reqIsIE = false;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.6.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.3.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Microsoft.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

alert('

ajax

 Requestor Fatal Error: Cannot instantiate an XMLHTTP Object');
}

ajax

 Requestor.prototype.__xmitLog = function(theMsg)
{
var bodyArr = document.getElementsByTagName('body');
var theBody = bodyArr[0];
theBody.appendChild(document.createTextNode(theMsg));
theBody.appendChild(document.createElement('br'));
}

ajax

 Requestor.prototype.__onRTS = function()
{
if ((this.requestor.readyState >= 2) && (this.timeoutHandle))
{
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
}

    if (this.requestor.readyState == 4)
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
if ((this.requestor.status==200) || (this.requestor.status==0))
{
this.lastResponse = this.__decodeString(this.requestor.responseText);
if (!this.lastResponse)
{
return false;
}
if (this.xmlHandler)
{
this.xmlHandler.importXML(this.lastResponse);
}
this.onSuccess(this);
} else {
switch(this.requestor.status)
{
case 12029:
case 12030:
case 12031:
case 12152:
case 12159:
// OK: It's the IE SSL bug. Create a tiemout to call <me> again...
var loader = this;
setTimeout( function() { loader.execute.call(loader); }, 10);
break;

default:
this.onError(this);
}
}
this.busy = false;
}
}

ajax

 Requestor.prototype.__postParams = function()
{
var out = "";
var varNames = '';
for (var i=0; i<this.postNames.length; i++)
{
if (i > 0) { varNames += '|'; }
varNames += this.postNames<>;
if (i > 0) { out += '&'; }
out += this.postNames<> + '=' + this.__encodeString(this.postValues<>);
}
if (out) { out += '&' + '

ajax

 _var_names=' + varNames; }
return out;
}

ajax

 Requestor.prototype.abort = function()
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
this.busy = false;
}
}

ajax

 Requestor.prototype.clear = function()
{
this.methodPost = true;
this.__transStatus = 0;
this.__transBusy = false;
    this.lastResponse = new String();
this.selfReference = null;
    this.newRequest();
this.timeoutHandle = false;
this.timeoutMS = 8000;
}

ajax

 Requestor.prototype.clearAll = function()
{
    this.xmlHandler = null;
    this.masterStatus = null;
    this.onUnrecognized = new String();

    this.onError = this.__defaultError;
    this.onSuccess = this.__defaultSuccess;
   
    this.clear();
}

ajax

 Requestor.prototype.execute = function(timeoutVal)
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
this.busy = false;
}

var thisTimeoutVal = this.timeoutMS;
if (timeoutVal != undefined) { thisTimeoutVal = timeoutVal; }

this.__getRequestor();

if (!this.requestor) {
alert("You cannot dispatch a request on this

mac

 hine (no viable XMLHTTPRequestor)");
return "";
}
if (!this.url) {
alert("You must supply a URL to

ajax

 Requestor to process a request");
return "";
}

this.busy = true;
var httpMethod = (this.methodPost) ? 'POST' : 'GET';

var theURL = this.url;
theURL += this.__getParams();
this.lastRequest = theURL;

var loader = this;
this.requestor.onreadystatechange = function() { loader.__onRTS.call(loader); }
if (this.masterStatus) { this.masterStatus.handleChange(true); }

// Set a callback to <me> in case the request takes to long...
this.timeoutHandle = setTimeout( function() { loader.__handleTimeout.call(loader); }, this.timeoutMS);

    this.requestor.open('POST', theURL, true);
    this.requestor.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   

    this.requestor.send(this.__postParams());

}

ajax

 Requestor.prototype.__handleAbort = function()
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
this.requestor.onreadystatechange = null;
this.requestor.abort();
}

ajax

 Requestor.prototype.__handleTimeout = function()
{
this.__handleAbort();
var loader = this;
setTimeout(function() { loader.execute.call(loader); }, 100);
}

ajax

 Requestor.prototype.getParam = function(key, value)
{
var ptr = this.getNames.length;
for (var i=0; i<this.getNames.length; i++)
{
if (this.getNames<> == key) { ptr = i; }
}
this.getNames[ptr] = key;
this.getValues[ptr] = value;
}

ajax

 Requestor.prototype.method = function(doPost)
{
this.methodPost = (doPost);
}

ajax

 Requestor.prototype.newRequest = function()
{
this.getNames = new Array();
this.getValues = new Array();
this.postNames = new Array();
this.postValues = new Array();
this.url = '';
}

ajax

 Requestor.prototype.postParam = function(key, value)
{
var ptr = this.postNames.length;
for (var i=0; i<this.postNames.length; i++)
{
if (this.postNames<> == key) { ptr = i; }
}
this.postNames[ptr] = key;
this.postValues[ptr] = value;
}

perkiset

Update: This is the

AJAX

  Requestor class with the fix for IE, related to the SSL and error number 12029, 12030, 12031, 12152 and 12159 issue. Note that I have commented out error handling specific to these errors, because if this patch does not completely handle it I'd really like to know.

/p


// ----------------------------------------------------------- //
//                     

ajax

 Requestor                        //
// ----------------------------------------------------------- //
function

ajax

 Requestor() { this.clearAll(); }

ajax

 Requestor.prototype.__defaultError = function(sender)
{
var tempStr = "

ajax

 Requestor Error: " +
                  "status: " + this.requestor.status + " " +
          "headers: " + this.requestor.getAllResponseHeaders();
alert(tempStr);
}

ajax

 Requestor.prototype.__defaultSuccess = function(sender)
{
alert("

ajax

 Requestor successfully returned from a request - but there is no handler assigned to receive it");
}

ajax

 Requestor.prototype.__decodeString = function(inputStr)
{
var decoded = unescape(inputStr);
decoded = decoded.replace(/\%2F/g, "/");
decoded = decoded.replace(/\%3F/g, "?");
decoded = decoded.replace(/\%3D/g, "=");
decoded = decoded.replace(/\%26/g, "&");
decoded = decoded.replace(/\%40/g, "@");
return decoded;
}

ajax

 Requestor.prototype.__encodeString = function(inputStr)
{
var encoded = escape(inputStr);
encoded = encoded.replace(///g,"%2F");
encoded = encoded.replace(/?/g,"%3F");
encoded = encoded.replace(/=/g,"%3D");
encoded = encoded.replace(/&/g,"%26");
encoded = encoded.replace(/@/g,"%40");
return encoded;
}

ajax

 Requestor.prototype.__getParams = function()
{
if (this.getNames.length == 0) { return ""; }
var out = (this.url.indexOf('?') == -1) ? '?' : '&';
for (var i=0; i<this.getNames.length; i++)
{
out += this.getNames<> + '=' + this.getValues<>;
if (i < (this.getNames.length - 1)) { out += '&'; }
}
return out;
}

ajax

 Requestor.prototype.__getRequestor = function()
{
if ((this.requestor != null) && (!this.reqIsIE)) { return true; }

try {
this.requestor = new XMLHttpRequest();
this.reqIsIE = false;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.6.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.3.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Microsoft.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

alert('

ajax

 Requestor Fatal Error: Cannot instantiate an XMLHTTP Object');
}

ajax

 Requestor.prototype.__xmitLog = function(theMsg)
{
var bodyArr = document.getElementsByTagName('body');
var theBody = bodyArr[0];
theBody.appendChild(document.createTextNode(theMsg));
theBody.appendChild(document.createElement('br'));
}

ajax

 Requestor.prototype.__onRTS = function()
{
if ((this.requestor.readyState >= 2) && (this.timeoutHandle))
{
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
}

    if (this.requestor.readyState == 4)
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
if ((this.requestor.status==200) || (this.requestor.status==0))
{
this.lastResponse = this.__decodeString(this.requestor.responseText);
if (!this.lastResponse)
{
return false;
}
if (this.xmlHandler)
{
this.xmlHandler.importXML(this.lastResponse);
}
this.onSuccess(this);
} else {
alert('ERROR: ' + this.requestor.status);
/*
switch(this.requestor.status)
{
case 12029:
case 12030:
case 12031:
case 12152:
case 12159:
// OK: It's the IE SSL bug. Create a tiemout to call <me> again...
var loader = this;
setTimeout( function() { loader.execute.call(loader); }, 10);
break;

default:
this.onError(this);
}
*/
}
this.busy = false;
}
}

ajax

 Requestor.prototype.__postParams = function()
{
var out = "";
var varNames = '';
for (var i=0; i<this.postNames.length; i++)
{
if (i > 0) { varNames += '|'; }
varNames += this.postNames<>;
if (i > 0) { out += '&'; }
out += this.postNames<> + '=' + this.__encodeString(this.postValues<>);
}
if (out) { out += '&' + '

ajax

 _var_names=' + varNames; }
return out;
}

ajax

 Requestor.prototype.abort = function()
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
this.busy = false;
}
}

ajax

 Requestor.prototype.clear = function()
{
this.methodPost = true;
this.__transStatus = 0;
this.__transBusy = false;
    this.lastResponse = new String();
this.selfReference = null;
    this.newRequest();
this.timeoutHandle = false;
this.timeoutMS = 8000;
}

ajax

 Requestor.prototype.clearAll = function()
{
    this.xmlHandler = null;
    this.masterStatus = null;
    this.onUnrecognized = new String();

    this.onError = this.__defaultError;
    this.onSuccess = this.__defaultSuccess;
   
    this.clear();
}

ajax

 Requestor.prototype.execute = function(timeoutVal)
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
this.busy = false;
}

var thisTimeoutVal = this.timeoutMS;
if (timeoutVal != undefined) { thisTimeoutVal = timeoutVal; }

this.__getRequestor();

if (!this.requestor) {
alert("You cannot dispatch a request on this

mac

 hine (no viable XMLHTTPRequestor)");
return "";
}
if (!this.url) {
alert("You must supply a URL to

ajax

 Requestor to process a request");
return "";
}

this.busy = true;
var httpMethod = (this.methodPost) ? 'POST' : 'GET';

var theURL = this.url;
theURL += this.__getParams();
this.lastRequest = theURL;

var loader = this;
this.requestor.onreadystatechange = function() { loader.__onRTS.call(loader); }
if (this.masterStatus) { this.masterStatus.handleChange(true); }

// Set a callback to <me> in case the request takes to long...
this.timeoutHandle = setTimeout( function() { loader.__handleTimeout.call(loader); }, this.timeoutMS);

this.theURL = theURL;
    this.requestor.open('POST', theURL, true);
    this.requestor.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  if ((document.all) && (document.getElementById))
  {
  // IE
  setTimeout( function() { loader.__executeSend.call(loader)} , 10);
  } else {
this.requestor.send(this.__postParams()); 
}
}

ajax

 Requestor.prototype.__executeSend = function() { this.requestor.send(this.__postParams()); }

ajax

 Requestor.prototype.__handleAbort = function()
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
this.requestor.onreadystatechange = null;
this.requestor.abort();
}

ajax

 Requestor.prototype.__handleTimeout = function()
{
this.__handleAbort();
var loader = this;
setTimeout(function() { loader.execute.call(loader); }, 100);
}

ajax

 Requestor.prototype.getParam = function(key, value)
{
var ptr = this.getNames.length;
for (var i=0; i<this.getNames.length; i++)
{
if (this.getNames<> == key) { ptr = i; }
}
this.getNames[ptr] = key;
this.getValues[ptr] = value;
}

ajax

 Requestor.prototype.method = function(doPost)
{
this.methodPost = (doPost);
}

ajax

 Requestor.prototype.newRequest = function()
{
this.getNames = new Array();
this.getValues = new Array();
this.postNames = new Array();
this.postValues = new Array();
this.url = '';
}

ajax

 Requestor.prototype.postParam = function(key, value)
{
var ptr = this.postNames.length;
for (var i=0; i<this.postNames.length; i++)
{
if (this.postNames<> == key) { ptr = i; }
}
this.postNames[ptr] = key;
this.postValues[ptr] = value;
}

thedarkness

Perk,

Noticed this syntax a lot in your code;

ajax

 Requestor.prototype.__decodeString

the double underscore specifically. Is this indicating a "private method" of the class?

Cheers,
td

perkiset

@ private method: by my spec only. I needed a way to denote privates in a world where there are none... so this is my personal way of remembering that I should not touch it publicly.

Fishing

Javascript

   Applause

/p

thedarkness

Hey perk,

Have you had a look at this;

http://

javascript

 .crockford.com/private.html

according to that trult private members can be defined within the constructor of an object.

Have you looked at this?

Cheers,
td

perkiset

I read it... His article comes from a single perspective on

Javascript

  classes & you really need to make a decision of you're going to used this form of construction or the prototype syntax.

Privileged/Constructor: variables defined in the constructor can only be seen within the constructor - that's avoided by creating your function inside the constructor as well - in which case those functions can see the "private" variables. The problem here is if you create this.something and then prototype something later you cannot access those variables. The problem with this is construction time and memory load. This form of constructor will create a new version of that block of code for every instance of the class, because the functions are essentially in <that> objects' heap space.

Prototyping: This method, which I choose to employ much more often, uses way less memory because it is consumed once for the code and then the only memory that is used for instances is the variables you create during the life of the object. This has the downside that all your this.something variables (instance variables/members/properties/whatever) are available to every prototyped function... but they're available to the outside world as well - they are all ostensibly public - as are all of your functions.

So: If you have one big class that you'll probably never create two of, then it's really OK to use the constructor method because that memory is going to get chewed up any way. If you're going to have more than one instance of a class it's prolly better to go the other way. If you're going to stay sane, it's probably better to choose one or the other  Applause

So there is clearly a reason for both - but my personal opinion is that both mechanisms are just a FISHED UP way of doing it and any argument otherwise from the

Javascript

 erati is rationalization for a poory architected language. Little choice in the matter though...

Thanks for the read though - helps to read others opinions on it.

/p

perkiset

Yet one more posting of the code.

As I mention in the

Ajax

  over SSL in IE 6 thread, this covers a small shitty where the requestor refuses to send out the packet - in this case the timeout feature aborts and redispatches the request. Note that the default timeout is 2500ms, which is set via the timeoutMS property.

I HOPE that this is the last time I'll be reposting this code... but you never frappin' know...  Applause


// ----------------------------------------------------------- //
//                       

ajax

 Requestor                         //
// ----------------------------------------------------------- //
function

ajax

 Requestor() { this.clearAll(); }

ajax

 Requestor.prototype.__defaultError = function(sender)
{
var tempStr = "

ajax

 Requestor Error: " +
                  "status: " + this.requestor.status + " " +
          "headers: " + this.requestor.getAllResponseHeaders();
alert(tempStr);
}

ajax

 Requestor.prototype.__defaultSuccess = function(sender)
{
alert("

ajax

 Requestor successfully returned from a request - but there is no handler assigned to receive it");
}

ajax

 Requestor.prototype.__decodeString = function(inputStr)
{
var decoded = unescape(inputStr);
decoded = decoded.replace(/\%2F/g, "/");
decoded = decoded.replace(/\%3F/g, "?");
decoded = decoded.replace(/\%3D/g, "=");
decoded = decoded.replace(/\%26/g, "&");
decoded = decoded.replace(/\%40/g, "@");
return decoded;
}

ajax

 Requestor.prototype.__encodeString = function(inputStr)
{
var encoded = escape(inputStr);
encoded = encoded.replace(///g,"%2F");
encoded = encoded.replace(/?/g,"%3F");
encoded = encoded.replace(/=/g,"%3D");
encoded = encoded.replace(/&/g,"%26");
encoded = encoded.replace(/@/g,"%40");
return encoded;
}

ajax

 Requestor.prototype.__getParams = function()
{
if (this.getNames.length == 0) { return ""; }
var out = (this.url.indexOf('?') == -1) ? '?' : '&';
for (var i=0; i<this.getNames.length; i++)
{
out += this.getNames<> + '=' + this.getValues<>;
if (i < (this.getNames.length - 1)) { out += '&'; }
}
return out;
}

ajax

 Requestor.prototype.__getRequestor = function()
{
if ((this.requestor != null) && (!this.reqIsIE)) { return true; }

try {
this.requestor = new XMLHttpRequest();
this.reqIsIE = false;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.6.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP.3.0");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Msxml2.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

try {
this.requestor = new ActiveXObject("Microsoft.XMLHTTP");
this.reqIsIE = true;
return; true;
} catch(e) {}

alert('

ajax

 Requestor Fatal Error: Cannot instantiate an XMLHTTP Object');
}

ajax

 Requestor.prototype.__xmitLog = function(theMsg)
{
var bodyArr = document.getElementsByTagName('body');
var theBody = bodyArr[0];
theBody.appendChild(document.createTextNode(theMsg));
theBody.appendChild(document.createElement('br'));
}

ajax

 Requestor.prototype.__onRTS = function()
{
if ((this.requestor.readyState >= 2) && (this.timeoutHandle))
{
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
}

    if (this.requestor.readyState == 4)
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
if ((this.requestor.status==200) || (this.requestor.status==0))
{
this.lastResponse = this.__decodeString(this.requestor.responseText);
if (!this.lastResponse)
{
return false;
}
if (this.xmlHandler)
{
this.xmlHandler.importXML(this.lastResponse);
}
this.onSuccess(this);
} else {
switch(this.requestor.status)
{
case 12029:
case 12030:
case 12031:
case 12152:
case 12159:
alert('Untrapped error: ' + this.request.status);
/*
var loader = this;
setTimeout( function() { loader.execute.call(loader); }, 10);
*/
break;

default:
this.onError(this);
}
}
this.busy = false;
}
}

ajax

 Requestor.prototype.__postParams = function()
{
var out = "";
var varNames = '';
for (var i=0; i<this.postNames.length; i++)
{
if (i > 0) { varNames += '|'; }
varNames += this.postNames<>;
if (i > 0) { out += '&'; }
out += this.postNames<> + '=' + this.__encodeString(this.postValues<>);
}
if (out) { out += '&' + '

ajax

 _var_names=' + varNames; }
return out;
}

ajax

 Requestor.prototype.abort = function()
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
clearTimeout(this.timeoutHandle);
this.timeoutHandle = false;
this.busy = false;
}
}

ajax

 Requestor.prototype.clear = function()
{
this.methodPost = true;
this.__transStatus = 0;
this.__transBusy = false;
    this.lastResponse = new String();
this.selfReference = null;
    this.newRequest();
this.timeoutHandle = false;
this.timeoutMS = 2500;
}

ajax

 Requestor.prototype.clearAll = function()
{
    this.xmlHandler = null;
    this.masterStatus = null;
    this.onUnrecognized = new String();

    this.onError = this.__defaultError;
    this.onSuccess = this.__defaultSuccess;
   
    this.clear();
}

ajax

 Requestor.prototype.execute = function(timeoutVal)
{
if (this.busy)
{
// clear timeout as well
this.requestor.abort();
this.busy = false;
}

var thisTimeoutVal = this.timeoutMS;
if (timeoutVal != undefined) { thisTimeoutVal = timeoutVal; }

this.__getRequestor();

if (!this.requestor) {
alert("You cannot dispatch a request on this

mac

 hine (no viable XMLHTTPRequestor)");
return "";
}
if (!this.url) {
alert("You must supply a URL to

ajax

 Requestor to process a request");
return "";
}

this.busy = true;
var httpMethod = (this.methodPost) ? 'POST' : 'GET';

var theURL = this.url;
theURL += this.__getParams();
this.lastRequest = theURL;

var loader = this;
this.requestor.onreadystatechange = function() { loader.__onRTS.call(loader); }
if (this.masterStatus) { this.masterStatus.handleChange(true); }

// Set a callback to <me> in case the request takes to long...
this.timeoutHandle = setTimeout( function() { loader.__handleTimeout.call(loader); }, this.timeoutMS);

    this.requestor.open('POST', theURL, true);
    this.requestor.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  if ((document.all) && (document.getElementById))
  {
  // IE
  setTimeout( function() { loader.__executeSend.call(loader)} , 10);
  } else {
this.requestor.send(this.__postParams()); 
}
}

ajax

 Requestor.prototype.__executeSend = function() { this.requestor.send(this.__postParams()); }

ajax

 Requestor.prototype.__handleAbort = function()
{
if (this.masterStatus) { this.masterStatus.handleChange(false); }
this.requestor.abort();
}

ajax

 Requestor.prototype.__handleTimeout = function()
{
this.__handleAbort();
var loader = this;
setTimeout(function() { loader.execute.call(loader); }, 100);
}

ajax

 Requestor.prototype.getParam = function(key, value)
{
var ptr = this.getNames.length;
for (var i=0; i<this.getNames.length; i++)
{
if (this.getNames<> == key) { ptr = i; }
}
this.getNames[ptr] = key;
this.getValues[ptr] = value;
}

ajax

 Requestor.prototype.method = function(doPost)
{
this.methodPost = (doPost);
}

ajax

 Requestor.prototype.newRequest = function()
{
this.getNames = new Array();
this.getValues = new Array();
this.postNames = new Array();
this.postValues = new Array();
this.url = '';
}

ajax

 Requestor.prototype.postParam = function(key, value)
{
var ptr = this.postNames.length;
for (var i=0; i<this.postNames.length; i++)
{
if (this.postNames<> == key) { ptr = i; }
}
this.postNames[ptr] = key;
this.postValues[ptr] = value;
}


Perkiset's Place Home   Politics @ Perkiset's