By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Javascript Function Question

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.