Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript OO and XMLHttpRequest

Status
Not open for further replies.

Bodger2

Programmer
Sep 17, 2004
4
US
I am trying to organize a request/response ajax thingy here and have written this.

function myAJAXRequest (url, sendOutputTo)
{
this.url = url;
this.myRequest = new XMLHttpRequest ();
this.sendOutputTo = sendOutputTo;
this.myResponseDoc = null;

function processReqChange ()
{
var state = myRequest.readyState;

alert ("STATE (" + state + ")");

if (state == 4)
{
myResponseDoc = myRequest.responseXML.documentElement;
if (sendOutputTo != null)
{
sendOutputTo (myResponseDoc);
}
}
}

this.myRequest.onreadystatechange = this.processReqChange;
this.myRequest.open("GET", this.url, true);
this.myRequest.send("");
}

My method processReqChange never gets executed, much less can I track the state changes. I have been able to get this to work outside of the OO style. I am a little weak on Javascript OO and am trying to not have a custom processReqChange script for each of my requests.

Thanx

Bodger
 
processReqChange() must be a method of the myAJAXRequest object.

Code:
function myAJAXRequest () {

  ...

  this.processReqChange = function () {
    ...
  }

  ...

}
 
Thank you for your replies.

mbrooks - I need a little more power then is described there. I am having trouble understanding and working with the OO aspects of Javascript in regards to this.

rac2

>this.processReqChange = function

works, my processReqChange gets called, but it does not have its object context. How can I preserve the object context, specifically I am desperate to be able to contain the myRequest object in the processReqChange function.

Is there anyway to get the function to access the myAJAXRequest context? Specifically the myRequest object?

I do not want to build a custom processReqChange for each deployment.

Thanx

Bodger
 
I have solved my problem.

My primary requirement is to access my object from within the processReqChange function that is called by XMLHttpRequest so that I can manage functionalilty.

Here is my solution:


function myAJAXRequest (url, sendOutputTo)
{
myRequest = new XMLHttpRequest ();

this.url = url;
this.sendOutputTo = sendOutputTo;
this.myRequest = myRequest;

function myProcessReqChange ()
{
var myAJAX = this.myAjax;
var myRequest = myAJAX.myRequest;
var state = myAJAX.myRequest.readyState;
var sendOutputTo = myAJAX.sendOutputTo;

if (state == 4)
{
myResponseDoc = myRequest.responseXML.documentElement;
if (sendOutputTo != null)
{
sendOutputTo (myResponseDoc);
}
}
}

myProcessReqChange.myAjax = this;

myRequest.onreadystatechange = myProcessReqChange;
myRequest.open("GET", this.url, true);
myRequest.send("");
}

This is so cool, I was able to make my object a property of the function and then am able to access that property and hence the object from within the function. This is so cool.

Bodger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top