noddy wrote:
>
What I thought was a string value does not seem to be recognised as
such.
In the following code I extract the string "foo" from an array and
put it into the variable up0
But on checking I find that
(up0=="foo") returns false.
Using the toString function as in
var up0 = update[0].toString();
makes no difference
This is part of a bare bones AJAX demonstration known as
Rasmus' 30 second AJAX Tutorial
The string "foo|foo done" is returned by http.responseText
The aim is to use it to locate <div id="foo"and to display "foo
done" as its innerHTML
The code returns an errot at
document.getElementById(up0)
A lot of the following lines of code are variables and alerts I have
introduced to try to pinpoint where the problem lies.
Here is the code in more detail followed by the results reported in
the alerts
========================:
function handleResponse() {
if(http.readyState == 4){
Hi,
Maybe your response contains more characters than you expect.
I would start by replacing the next line to:
var response = http.responseText;
to
var response = 'foo|foo done';
var update = new Array();
if(response.indexOf('|' != -1)) {
Tom said already the above line is wrong.
Do you want to have the indexOf
'|'
or do you want to have the indexOf of whatever
'|' != -1
produces?
I think '|' != -1 always evalutes to 'true'.
so you are effective looking for indexOf('true') which returns -1, which
evaluates in your if() to true.
Here follows a slightly modified version of your script that behaves as
expected:
<html>
<body>
<div id="update0" </div>
<div id="update1" </div>
<script type="text/javascript">
var response = 'foo|foo done';
var update = new Array();
if(response.indexOf('|') != -1) {
update = response.split('|');
// the following lines are for debugging
// and not part of the original code
var lgth = update.length;
alert(" ALERT 0 - respose string is " + response);
alert(" ALERT 1 - update length is " + lgth);
var up0 = update[0];
var up1 = update[1].toString();
alert( " ALERT 2 - " + up0 + " and " + up1 ) ;
if(up0=="foo"){
alert(" ALERT 3 - up0 is foo");
}else{
alert(" ALERT 3 - up0 is NOT foo");
}
if(up1=="foo done"){
alert(" ALERT 4 - is done");
} else{
alert(" ALERT 4 - is NOT foo done");
}
document.getElementById("update0").innerHTML = "update[0] = "+update[0];
document.getElementById("update1").innerHTML = "update[1] = "+update[1];
// debugging code ends
}
</script>
</body>
</html>
Regards,
Erwin Moller