Javascript object function problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Javascript object function problem

  1. #1
    sean Guest

    Javascript object function problem


    Hi all,
    I have a probelm I am trying to work around.

    In the code below the id proprty is not visible in the "loadedFnc" function.

    I am trying to do something asynchronously, thus am setting the onreadystatechange
    property to be a function in the javascript object. However when I do this
    the "this" does not see the properties of my object.

    Am I being a little ambitous?

    Regards
    Sean

    function xmlCache(pid)
    {
    this.id = pid;
    this.load = xmlCacheLoad;
    var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    this.HTTP = xmlHttp;
    }
    function xmlCacheLoad()
    {
    this.HTTP.onreadystatechange = this.loadedFnc;
    }

    function loadedFnc()
    {
    alert(this.id);
    {
    //do something
    }
    }

  2. #2
    Kevin Guest

    Re: Javascript object function problem


    Sean,

    Perhaps you could try calling the loadedFnc function and passing in "this"
    as a parameter like so:

    this.HTTP.onreadystatechange = loadedFnc(this);

    Then set your function to accept parameters such as:

    function loadedFnc(obj){
    alert(obj.id);
    return obj.id;
    }

    I don't know for certain that this will work, but it should give you some
    perspective on other ways to look at the problem.

    Kevin
    Webmaster - JavaScript Solutions
    http://www.javascriptsolutions.com



    "sean" <sean@ireland.com> wrote:
    >
    >Hi all,
    >I have a probelm I am trying to work around.
    >
    >In the code below the id proprty is not visible in the "loadedFnc" function.
    >
    >I am trying to do something asynchronously, thus am setting the onreadystatechange
    >property to be a function in the javascript object. However when I do this
    >the "this" does not see the properties of my object.
    >
    >Am I being a little ambitous?
    >
    >Regards
    >Sean
    >
    >function xmlCache(pid)
    >{
    > this.id = pid;
    > this.load = xmlCacheLoad;
    > var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    > this.HTTP = xmlHttp;
    >}
    >function xmlCacheLoad()
    >{
    > this.HTTP.onreadystatechange = this.loadedFnc;
    >}
    >
    >function loadedFnc()
    >{
    >alert(this.id);
    >{
    >//do something
    >}
    >}



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center