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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SOLVED: How to pass a function/variable-reference to XMLHttprequest-onload-event?

    Hello!
    How to pass a function- or variable-reference to the xmlhttprequest-onload-event? The function and the variable are part of an object genereated with "tempObj = new Seite(...)" .
    I should add, that this will only running locally on Firefox.
    The error i get in the Javascript-Console is "this.callMeFromOnLoad is not a function"

    Here is an example-page:
    Code:
    <html>
    <head>
    <title>Why are the lines marked by "//<===" not working? How to pass that function call to the function onLoad?</title>
    <script language="javascript" type="text/javascript">
    Seite = function(fuURL,fuText) {
      this.status = 0; this.theURL = fuURL; this.myId = fuText;
      this.HTMLofURL = 'placeholder-text';
      function callMeFromOnLoad(aURL,aHTMLsource) { alert(this.myId); }
      function onLoad (e)    {
          this.HTMLofURL = e.target.responseText;  //<=== "this.HTMLofURL" will actually not be the "this.HTMLofURL" as two lines above, when called from the
                                                   // XMLHttoRequest onload-event ):
          testAlert(this.theURL + "\n\n" + this.HTMLofURL);  // "this.theURL" isn't the same as the "this.theURL" from above
          this.callMeFromOnLoad(this.theURL,this.HTMLofURL); //<=== same here
      }
      this.LoadPage = function() {
        try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) {   alert("Permission UniversalXPConnect denied."); }
        var r = new XMLHttpRequest();
        r.onload = onLoad;
        r.open ("GET", this.theURL, true);
        r.send (null);
      }
    }
    function testAlert(fuText) { alert(fuText);}
    function testSearch() {
      var tempObj;
      tempObj = new Seite('http://www.google.com','TestObj1');
      tempObj.LoadPage();
    }
    </script>
    </head>
    <body onLoad="testSearch();">
    Hello
    <pre>
    How to pass a function- or variable-reference to the xmlhttprequest-onload-event? The function and the variable are part of an object genereated with "tempObj = new Seite(...)" .
    Why are the lines marked by "//<===" not working? How to pass that function call to the function onLoad?
       this.HTMLofURL = e.target.responseText;  //<=== "this.HTMLofURL" will actually not be the "this.HTMLofURL" as two lines above, when called from the
                                                // XMLHttoRequest onload-event ):
       testAlert(this.theURL + "\n\n" + this.HTMLofURL);  // "this.theURL" isn't the same as the "this.theURL" from above
       this.callMeFromOnLoad(this.theURL,this.HTMLofURL); //<=== same here
    </pre>
    <span id="id:forFastOutput"></span>
    </body></html>

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
    var req = this;
    function onLoad (e)    {
          req.HTMLofURL = e.target.responseText;  
         ...
    }
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow! I will test it.
    But I will only believe it, after i have tested it. :P

    Quote Originally Posted by A1ien51 View Post
    Code:
    var req = this;
    function onLoad (e)    {
          req.HTMLofURL = e.target.responseText;  
         ...
    }
    Eric
    If this really is a solution, that would be amazing, taking into account that I used up around 1 day and crawled through different javascript-sources and forums - where I only found one thread that might contain a solution, though not directly for what i asked for.

    Dieter

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    !!!! TYVM!!!!
    Me nearly came some tears.
    I'm not gay, but I would kiss you now, if you were in reach. :P


    Quote Originally Posted by A1ien51 View Post
    Code:
    var req = this;
    function onLoad (e)    {
          req.HTMLofURL = e.target.responseText;  
         ...
    }
    Eric


  •  

    Posting Permissions

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