471,887 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

Javascript Function Question

Hello,
I have a function called stateChanged:
function stateChanged()
{
alert(xmlHttp.readyState)
if (xmlHttp.readyState==4)....................
...............................
}

When I call it with:
xmlHttp.onreadystatechange=stateChanged()
I get an error: Type mismatch.
When I call it with
xmlHttp.onreadystatechange=stateChanged
It runs fine.
Whats wrong with the parenthesis in the function call?
Thanks
Mike
Jun 27 '08 #1
1 956
Mike wrote:
I have a function called stateChanged:
function stateChanged()
{
alert(xmlHttp.readyState)
Should be

window.alert(xmlHttp.readyState);

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.readyState==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.onreadystatechange=stateChanged()
I get an error: Type mismatch.
When I call it with
xmlHttp.onreadystatechange=stateChanged
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>
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.