DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  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
    >}
    >}



Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links