Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
-7
rated 0 times [  0] [ 7]  / answers: 1 / hits: 114418  / 15 Years ago, thu, may 14, 2009, 12:00:00

How could the following code sometimes evaluate to false?



(transport.responseText == '1' || 
transport.responseText == 'CARD_VALID')


My JavaScript code:



if (transport.responseText == '1' || 
transport.responseText == 'CARD_VALID') {
// do something.
}
else if (transport.responseText == 'CARD_INVALID' ||
transport.responseText == 'INVALID_CHECKSUM') {
// do something else....
}
else {
new Ajax.Request('/report_error.php?responseText='+transport.responseText);
// report error to user
}


What could cause JavaScript string compare == to return false when the strings are identical?


More From » string

 Answers
5

Double equals is the appropriate way to compare strings in Javascript, it is returning false then there may be whitespace to the left and or right of one string.



Put a .trim() on the end of the strings and my comparison should started working:



var panel = response.substr(0, response.indexOf(<)).trim();
if(panel == combo){
//do something
}

[#99538] Saturday, May 9, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
seth

Total Points: 307
Total Questions: 114
Total Answers: 96

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
seth questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Thu, Dec 24, 20, 00:00, 3 Years ago
Sun, Apr 19, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;