Hi,

this sometimes happens in other programming languages too. What you
can do is try something like: t=this; or var t=this;
The second solution would make a local reference to 'this' (the first
t would be global). So in the onChange function you could try
t.testVar.
>From what i remember about Javascript t should only be a reference to
the actual object, so it will be an up-to-date object.

Hope this helps,
Teo

On Sep 2, 6:29 pm, Daidalos <[email protected]> wrote:
> Hi all,
>
> I have a bit of a problem with the onreadystatechange function of the
> XMLHttpRequest.
>
> I have an object with methods to download an image and to do something
> with it after the download is finished.
> The downloading is working fine, but when the onreadystatechange
> function is called the variable "this" does not point to the object.
> As if the method was somehow called "outside" the object.
>
> To clearify see the following code (it is just a sample to demostrate
> the problem):
>
>
>
>
>
> > function createObject(){
> >    this.testVar = "test text";
> >    this.testDownload = function (){
> >            this.req = new XMLHttpRequest();
> >            this.req.open("GET", "http://www.google.com/webhp";, true);
> >            this.req.onreadystatechange = this.onChange;
> >            this.req.send();
> >    };
> >    this.onChange = function (){
> >            alert(testObject.testVar);
> >            alert(this.testVar);
> >              alert(typeof(this));
> >              alert(typeof(this.testVar));
> >    }
> > }
> > function view_onOpen() {
> >    testObject = new createObject();
> >    testObject.testDownload();
> > }
>
> When the function view_onOpen() is executed the first alert contains
> the expacted "test text" but the second alert is just blank. typeof
> (this) retruns "object" but typeof(this.testVar) returns "undefined".
>
> What can I do to make it work, does anybody have an idea?
>
> thanks and best regards
> -Viktor
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Desktop Developer Group" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-desktop-developer?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to