Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    XMLHttpRequest onreadystatechange in an object

    Hello everyone,

    I'm new to javascript so I'm trying all kinds of stuff to get a hang of it. I'm used to OOP so that's what I'm trying to do in javascript. It's been going well but now I got stuck at some point. Here's the object I made:
    Code:
    function objTest() {   
    
       this.intSomeVar = null;
       this.ajaxRequest = null;
    	
       this.setTheVar = function(intValue) {
          this.intSomeVar = intValue;
       }
    	
       this.getTheVar = function() {
          return this.intSomeVar;
       }
    	
       this.doubleTheVar = function(intValue) {
          var intTemp = this.getTheVar();
          intTemp = intTemp * 2;
          this.setTheVar(intTemp);
       }
    	
      this.getVarFromFile = function() {
        this.ajaxRequest = GetXmlHttpObject(); //<- some function thats creates the XMLHttpRequest object, it works
          	
        this.ajaxRequest.onreadystatechange = function() {
           if(this.ajaxRequest.readyState == 4){  //<- not working :(
              this.setTheVar(this.ajaxRequest.responseText);
           }
        }
        
        this.ajaxRequest.open("GET", "somevar.txt", true);
        this.ajaxRequest.send(null); 
      }
    }
    The function in bold is the one that doesn't work right. Now, I guess the problem is that I'm "creating" that callback function in there, so it's not part of the object, therefore I cant use any methods or properties in the object. How should I go about doing this?

    Thanks in advance!

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    I can't test this but I think it's right:
    Code:
    this.getVarFromFile = function() 
    {
      if( (this.ajaxRequest = GetXmlHttpObject()) ) //assuming it returns null on failure
      {
        this.ajaxRequest.onreadystatechange = (function(obj) 
        {
         return function()
         {
           if(obj.ajaxRequest.readyState == 4)
            obj.setTheVar(obj.ajaxRequest.responseText);
         }
        })(this);
      }
    }
    You're welcome in advance.

  • Users who have thanked Arty Effem for this post:

    Urmell (08-14-2008)

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Now there's something I'd never be able to figure out myself It works, thanks alot!

  • #4
    SSJ
    SSJ is offline
    Regular Coder
    Join Date
    Mar 2007
    Posts
    230
    Thanks
    0
    Thanked 4 Times in 4 Posts
    What you exactly want to achieve by this code?

  • #5
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SSJ View Post
    What you exactly want to achieve by this code?
    Basically.. knowledge of javascript I know the whole code is useless, I just wrote it to test creating an object, using variables in an object, calling methods in an object (and from within an object), assigning values to a variable with a method etc. Just my method of learning a new programming/scripting language; doing rather than just reading.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •