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
    Dec 2008
    Location
    Western Belgium
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Function to onreadystatechange as parameter

    Hello everybody,

    I like to use onreadystatechange inside a function as given parameter. Is there any way to do it?

    Code:
    MyFunction(funk)
    {
    ...
    req.onreadystatechange=funk;
    ...
    }
    
    function Call_to_my_2_function() 
    { 
        if(req..readyState == 4) { 
           alert(req.responseText);
        }
    }
    
    ...
    MyFunction('Call_to_my_2_function')
    ...
    Hope you understand what I mean.

  • #2
    New Coder
    Join Date
    Mar 2009
    Location
    Fabric Covered Box
    Posts
    69
    Thanks
    1
    Thanked 16 Times in 14 Posts
    Sure, you can pass a function reference -- just lose the quotes
    Code:
    function MyFunction(funky)
    {
    this.canYouSay=funky;
    this.yo=function(f){f.call(this)};
    this.toString=function(){return "so cool!"};
    return this;
    }
    function speak(s){alert(s)};
    function say_my_name(){alert(this)};
    
    like=new MyFunction(speak); 
    like.canYouSay("woof!"); 
    like.yo(say_my_name);

  • Users who have thanked Shannon Blonk for this post:

    b_r_h (07-14-2009)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,380
    Thanks
    11
    Thanked 592 Times in 572 Posts
    as long as req is a global, that would work as posted.
    otherwise, define Call_to_my_2_function inside of MyFunction, so they can share...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    New Coder
    Join Date
    Dec 2008
    Location
    Western Belgium
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thank's!

    I got it working when req is global.

    rnd me: Can you clarify to me by example, when it is not global?


  •  

    Posting Permissions

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