Javascript Ajax problem

Hello,

I am doing a value check via Ajax.
That is after the user enters for example their nickname for sign-up, then using Ajax I am checking to see if this nickname is already taken or not.

The Ajax part is working just fine.
However when the value is returned to the Javascript for processing it is not following logic at all.
To be exact here is the code:

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
	var result = xmlhttp.responseText;
	if (result == "match_found")
	{
		document.getElementById("warningdisplay").innerHTML='This Nick Name is already Taken';
		document.getElementById("nick").value = "";
		document.getElementById("nick").focus();
	} else {

		confirm(result);
	}

  }
}

But when the result is returned as “match_found”, the 1st part of the if is not getting executed but instead the else part is executed!!!
In fact the confirm(result); confirms that the value of result is “match_found”!!!
What the HEK is going on??

Regards,

Certain characters aren’t easily detected visually. For example, whitespace characters. Check the length of the string to see if it matches your assumption.

Hi,

I am not sure what you mean by that!
I mean these chars are exactly the same, in fact I am copying & pasting
from one program to the other program (code) to make sure they are same.

Anyway, what I did to get around this Javascript bug is to return numbers
rather than text. And using numbers for matching, then it works fine.
Of course it would be preferable if I was returning string since then they
are easier to read, but go to move on.

Bot I really hate Javascript :slight_smile:
I mean it gives you no Error messages as in Php, etc. and it behaves
in strange ways as with this case.

Cheers,

What would you think the result should be?


str1 = "hi";
str2 = "hi ";
alert(str1 == str2);

Well of course in this case the result of the == should be false since there is
an extra space in str2, but that is not the case in the original example that I listed here.

I am not sure the solution to your problem, but I do know if you used jQuery it would be 100% easier:

:slight_smile:

I’m not trying to argue that it is, or isn’t the problem, although it’s definitely a strong possibility. But, I’m more trying to show you that the method you used to to verify that “the strings really are the same!” is faulty. This is why I suggested checking the length of the string as the next step in debugging your problem.

I mean these chars are exactly the same, in fact I am copying & pasting
from one program to the other program (code) to make sure they are same.

You assume far too much even though you have strong evidence that your assumption is incorrect.

Most programming bugs/errors/whatever are rooted in the programmer making faulty assumptions. Debugging is the process of carefully verifying your assumptions.

Using alert/confirm etc… like you did is usually a pretty decent debugging strategy. But I was trying to demonstrate where they are deficient. Also, due to different ways to visually represent characters, and various character encodings, sometimes a string might look the same, but actually be a very different value. Checking the length usually reveals that there’s a difference. But sometimes you need to go so far as to do a hex dump.

You actually are making a very good argument as to what a crappy language Javascript is. That is in any other language I have worked with, you do not need to do a “hex dump” each time you want to compare one string with another. Also of course the other problem with Javascript is that it does not give you any debugging error messages!

But anyway, it is what it is and we have to I guess live with it, or do we :slight_smile:

How would have Jquery made this any easier!

You absolutely need to in other programming languages, for the exact same scenarios that you do with javascript.

This isn’t a javascript issue. This is a programmer issue. You just don’t understand yet, and seem reluctant to learn why this will be a problem for you with any programming language.

Well I have not had this problem with any other programming language!

You will soon.