/    Sign up×
Community /Pin to ProfileBookmark

ajax function question

see the function handlestatechange, i’d like to pass the variable ‘day’ from my startrequest function to the handlestate function. the problem is that when handlestatechange is called, it’s not called like a normal function with the ()

without the (), how else would i get an external variable into the handlestatechange function?

[CODE]function startRequest(day, month) {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(“GET”, “eventDetails.php?day=”+day+”&month=”+month+”&teamid=<?echo $teamid;?>”, true);
xmlHttp.send(null);

}

function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
alert(“The server replied with: ” + xmlHttp.responseText);
}
}
}[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManAug 13.2007 — see the function handlestatechange, i'd like to pass the variable 'day' from my startrequest function to the handlestate function. the problem is that when handlestatechange is called, it's not called like a normal function with the ()[/quote]

based on my own experiences, you can give the handlestatechange function parameters. for example:

[CODE]function startRequest(day, month) {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange[COLOR="Red"](day)[/COLOR];
xmlHttp.open("GET", "eventDetails.php?day="+day+"&month="+month+"&teamid=<?echo $teamid;?>", true);
xmlHttp.send(null);

}



function handleStateChange([COLOR="Red"]day[/COLOR]) {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
alert("The server replied with: " + xmlHttp.responseText);
}
}
}[/CODE]


however, realize that DAY will no longer be dynamic. if day is "13" when you initially pass it to that function, it will remain "13" each time handleStateChange is called.

you could also create a global scope variable and set day to it whenever you call the startrequest function.

however, i'd personally suggest creating a new property for your xmlHttp object called "day", and setting it to the day that you pass in though the function call. notice my test example:

&lt;html&gt;
&lt;body&gt;

&lt;script type="text/javascript"&gt;
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}

<i> </i>[COLOR="Red"]xmlHttp.testProperty = "test!"[/COLOR]

<i> </i>//xmlHttp.open("GET","time.asp",true);
<i> </i>//xmlHttp.send(null);

<i> </i>[COLOR="Red"]alert(xmlHttp.testProperty)[/COLOR]
}
&lt;/script&gt;

&lt;form name="myForm"&gt;
Name: &lt;input type="text"
onkeyup="ajaxFunction();" name="username" /&gt;
Time: &lt;input type="text" name="time" /&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@ixxalnxxiauthorAug 13.2007 — i've tried adding the ( ) to handlestatechange without even passing any parameters, but once i add the ( ) the ajax stops working altogether.
Copy linkTweet thisAlerts:
@A1ien51Aug 13.2007 — Your xmlHttp is sitting in a global varaible, so why don;t you just throw you day into a global variable and read that?

In reality you should learn about OO JavaScript.

Eric
Copy linkTweet thisAlerts:
@A1ien51Aug 13.2007 — Some info here:

var x = someFunction() means to execute now and store its returned value into this variable;

var x = someFunction means to assign that function to that variable

IEric
×

Success!

Help @ixxalnxxi 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.16,
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,
)...