472,961 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 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 982
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.