/    Sign up×
Community /Pin to ProfileBookmark

Why is my function acting weird ? plz help !

The function below has 2 alert boxes. When I run it, sometimes the outer alert box comes empty. Please explain in a human manner why .

[CODE]

function ItemCount()//returns the number of items in carousel
{

if (window.XMLHttpRequest) {
var req = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
var req = new ActiveXObject(“Microsoft.XMLHTTP”);
}
else
{
alert(“upgrade browser”);

}

req.open(“POST”, “/includes/_process/getPortfolioCount.php”, true);
req.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
req.send(null);
req.onreadystatechange = function() {
if ((req.readyState == 4) && (req.status == 200)) {

if (req.responseText.length) {

document.getElementById(‘portfolioCount’).value=req.responseText;

alert(req.responseText);

}

}
}

alert(“out:”+document.getElementById(‘portfolioCount’).value);

return document.getElementById(‘portfolioCount’).value;

}//function

[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarAug 29.2008 — I'm assuming this comes down to you making a variable in local scope, then not being able to refer to it outside of that scope(when you get the response back).

I'd say make the variable global.
Copy linkTweet thisAlerts:
@cgishackAug 29.2008 — Because you have assigned an anonymous function to the onreadystatechange event. The code within that block will only get fired when that even happens.

So, the alert("out:"+document.getElementById('portfolioCount').value);

code will only contain a value if a value is assigned to it, which probably before the onreadystate event occurred.

Does that make sense ?

Drew
Copy linkTweet thisAlerts:
@2levelsaboveauthorAug 29.2008 — I completely understand and thanks so much for the response.

Now, how can I make sure that the alert("out:"+document.getElementById('portfolioCount').value);

statement gets assigned the value AFTER the onchange event.


Simply put all I am trying to do is to get the value obtained from the ajax call. I was not able to access the req.responseText outside the scope so I decided to assign that value to a hidden form element.

grah!!!!!
Copy linkTweet thisAlerts:
@konithomimoAug 29.2008 — put it inside th new function. This means make this

}

<i> </i>alert("out:"+document.getElementById('portfolioCount').value);




<i> </i>return document.getElementById('portfolioCount').value;


this:


alert("out:"+document.getElementById('portfolioCount').value);
}



<i> </i>return document.getElementById('portfolioCount').value;
×

Success!

Help @2levelsabove 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.15,
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,
)...