/    Sign up×
Community /Pin to ProfileBookmark

JS function calls, difference, with and without brackets????

Hello again,

I was wondering why I get a different status result when I call a funtion in js with and without brackets and vars. In particular if I call processChange without brackets I get a ready state of 4 and status of 200, if I call with () or if I try to pass a variable in after adding this to the function processChange I get 0,0 for ready state and status??????

Thanks in advance,

Leander

[code] if (req != null) {
req.onreadystatechange = processChange;
req.open(“POST”, url, true);
req.send(null);
}
}
function loadXMLDoc2(url) {

loadXMLDoc(url);
}

function processChange() {

// The page has loaded and the HTTP status code is 200 OK
if (req.readyState == 4 && req.status == 200) {

// Write the contents of this URL to the south_east_box div

getObject(p_name).innerHTML = req.responseText;

}
}
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@nzakasMay 09.2005 — Consider this line of code:

req.onreadystatechange = processChange;

This is actually not calling processChange, but rather, assigning the onreadystatechange property to [I]point to[/I] processChange (whenever you use a function name without parentheses, it acts like a regular variable). This is the correct way of doing it.

If you change it to this:

req.onreadystatechange = processChange();

What you are doing is calling processChange() and then returning the result of the function (undefined) into the onreadystatechange property. So, the function is being called before the request is made, thus the readyState and status are both equal to 0.
×

Success!

Help @soldierdog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...