Advertisement

09.27.2007 at 08:40AM PDT, ID: 22856987
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

Have XmlHttpRequest Object keep reference to this in onreadystatechange function

Asked by grav0005 in JavaScript, WebApplications

Tags: , ,

Hi,

As you're probably aware, in IE 6, when you assign a function to the XmlHttpRequest onreadystatechange event, you lose the reference to the object.  For example, notice how in the function below I have to refer to my xmlhttp object by name, instead of simply saying this.readystate and this.status. :

xmlhttp.onreadystatechange = function()
{      
    if (xmlhttp.readyState==4)
    {
        if ( xmlhttp.status == 200 )
        {      
                //do stuff
        }              
    }
}
Is there a workaround that would allow me to use the this keyword to reference the XmlHttpRequest object.  This is especially a problem in IE 6 because it is an ActiveX object.

I found some code for a wrapper class at http://www.allegro.cc/forums/thread/591774 but I'm not entirely sure how to do it.  It looks somewhat complicated. :-)

// inside the RPC.transport constructor
// 'this' is the RPC instance
this.obj = new XMLHttpRequest();  
if (this.obj)
{
  this.obj.onreadystatechange = Delegate.create(this, 'onReadyStateChange');
}(FYI: For IE, I define the XMLHttpRequest function to return the ActiveX control.)

You're right that you lose the "this" pointer in the onReadyStateChange function if you just do an anonymous function. That's why I use a Delegate to keep track of things. I created this method myself; there may be some other defacto standard way of doing this.

Later on:
RPC.transport.prototype.onReadyStateChange = function()
{
  // this.obj points to the XMLHttpRequest object.

  // eventually:
  else if (this.responseType == RPC.JSON)
  {
    eval("var json = " + this.obj.responseText);
    this.onLoad(json);
  }
}My Delegate class looks like:
/*
        Copyright 2006-07 by Matthew Leverton

      Delegate: a static class to help keep events tied to an object.
*/
var Delegate = {
      objects: new Array(),
      
      /*
            (object) ptr: the object the event should be tied to
            (string) func: the name of the function that should be called; it must reside in the ptr object.
      */
      create: function(ptr, func)
      {
            if (!ptr._delegates)
            {
                  ptr._delegates = new Array();
                  Delegate.objects[Delegate.objects.length] = ptr;
            }
            
            return !ptr._delegates[func] ? (ptr._delegates[func] = function(event) { return ptr[func](event); }) : ptr._delegates[func];
      },
      
      /*
            Returns a delegate already created. Note that subsequent calls to 'create' are the
            same as calling get, so this method really doesn't ever _need_ to be used.
      */
      get: function(ptr, func)
      {
            return (!ptr._delegates || !ptr._delegates[func]) ? false : ptr._delegates[func];
      },
      
      /*
            Destroys the delegate (but not the actual function). It can be useful in removing
            small memory leaks in some browsers, but doesn't have to be called.
      */
      destroy: function(ptr, func)
      {
            if (!ptr._delegates || !ptr._delegates[func]) return false;
            
            ptr._delegates[func] = null;
      },
      
      /*
            Destroys all the delegates. (See the destroy method.)
      */
      destroyAll: function()
      {
            for (var i in Delegate.objects)
            {
                  for (var j in Delegate.objects[i]._delegates)
                  {
                        Delegate.objects[i]._delegates[j] = null;
                  }
            }
      }
};

Thanks,

Rob
            Start Free Trial
[+][-]09.27.2007 at 12:28PM PDT, ID: 19974053

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.27.2007 at 12:33PM PDT, ID: 19974104

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.28.2007 at 11:00AM PDT, ID: 19980507

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.28.2007 at 12:16PM PDT, ID: 19981055

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.28.2007 at 12:18PM PDT, ID: 19981070

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.02.2007 at 06:13AM PDT, ID: 19998213

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.03.2007 at 06:42AM PDT, ID: 20006419

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.11.2007 at 08:53AM PDT, ID: 20058403

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JavaScript, WebApplications
Tags: object, destroy, onreadystatechange
Sign Up Now!
Solution Provided By: Badotz
Participating Experts: 1
Solution Grade: A
 
 
[+][-]10.11.2007 at 12:45PM PDT, ID: 20060425

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42 / EE_QW_2_20070628