Javascript Function Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    Javascript Function Question

    Hello,
    I have a function called stateChanged:
    function stateChanged()
    {
    alert(xmlHttp.r eadyState)
    if (xmlHttp.readyS tate==4)....... .............
    ............... ............... .
    }



    When I call it with:
    xmlHttp.onready statechange=sta teChanged()
    I get an error: Type mismatch.
    When I call it with
    xmlHttp.onready statechange=sta teChanged
    It runs fine.
    Whats wrong with the parenthesis in the function call?
    Thanks
    Mike
  • Thomas 'PointedEars' Lahn

    #2
    Re: Javascript Function Question

    Mike wrote:
    I have a function called stateChanged:
    function stateChanged()
    {
    alert(xmlHttp.r eadyState)
    Should be

    window.alert(xm lHttp.readyStat e);

    and presumably the whole thing should not be a Function statement but a
    Function expression that creates a closure (else you need globals which are
    very bad style, or a wrapper object that I don't see here).
    if (xmlHttp.readyS tate==4)....... .............
    Don't forget to test the `status' property as well. A response may be fully
    received but indicate an error.
    ............... ...............
    Your dot key is malfunctioning.
    }
    >
    When I call it with:
    xmlHttp.onready statechange=sta teChanged()
    I get an error: Type mismatch.
    When I call it with
    xmlHttp.onready statechange=sta teChanged
    It runs fine.
    Because that is _not_ a call, it is only an assignment expression, as expected.
    Whats wrong with the parenthesis in the function call?
    This is about a *callback*. The call itself must be made by the
    XHR implementation (which "knows" about the status change of the
    request-response-chain), not by you. Calling the function yourself
    results in the property being assigned the *return value* of the
    function, which so far is `undefined'.

    I suppose a value of the object type is expected there, `null' or
    a Function object reference, instead.

    See also http://developer.mozilla.org/en/docs/AJAX


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

    Comment

    Working...