...

View Full Version : Comparing string



cardician
02-04-2007, 03:02 AM
Hopefully someone here can clear this up. I've scoured the internet and can't for the life of me tell why this isn't working. All I'm trying to do is compare two strings in JavaScript. Here is the code I'm using.



document.getElementById(control_id).innerHTML=http_request.responseText;
var strTemp = http_request.responseText.valueOf();
var strReq = "InputValid";
if(strReq.valueOf() == strTemp.valueOf()){
alert("matches");
} else {
alert("does not match");
}


I've tried all kinds of variations of this, without the valueOf(), comparing the literal string "InputValid" to the variable, etc. No matter what I do the alert stating 'does not match' appears. Any thoughts?

david_kw
02-04-2007, 03:58 AM
document.getElementById(control_id).innerHTML=http_request.responseText;
var strTemp = http_request.responseText;
var strReq = "InputValid";
alert('"' + strTemp + '"');
if (strReq == strTemp) {
alert("matches");
} else {
alert("does not match");
}


This should work assuming that http_request.responseText has the string "InputValid" in it. You can add the alert to check it. The valueOf() shouldn't be necessary at all.

david_kw

cardician
02-04-2007, 04:29 AM
Thanks for the response. I took your suggestion and did some more investigation with it. Still doesn't work but I've got a little more information. First of all, it does work in IE I discovered. I've been mostly testing in the latest version of Firefox and there it still doesn't work. I have the exact code that you have above, here is the result I now get from the alert statement in Firefox.

"
InputValid"

Exactly like that. So there is a " on one line, and then below it exactly is the rest of the string. Hence why its not matching in Firefox. Any ideas why this might be? Somehow Firefox seems to be adding a newline or something but IE doesn't. Suggestions are most welcome, thank you.

cardician
02-04-2007, 04:32 AM
Sorry to double post, but I think I've fixed it. I added this line


string = string.replace(/(^\s+|\s+$)/g,'');

in order to remove spaces from the string and it seems to have worked. I'm not sure how a space gets in there in Firefox but not IE. If anyone has any thoughts on that I'd really appreciate it. Thank you.

Kor
02-05-2007, 11:36 AM
If your string is in fact a HTML code, well, yes, IE and Moz considers the "gaps" between the tags in different ways. Moz counts all the gaps, while IE counts only the "logical HTML possible" gaps, as possible textNodes. This might be the reason for that inconsistency.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum