Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Comparing Strings


  • From: nick at nickfitz.co.uk (Nick Fitzsimons)
  • Subject: [Javascript] Comparing Strings
  • Date: Thu Aug 25 04:31:09 2005

> So the following is always false when it should be true practically all
> the time
>
> if (message_text == messages.innerHTML){
>
> I know I must be missing something, they both are strings (according
> to typeof) and I know the contents are the same cause the AJAX is
> returning the same thing every time.

It would help if you could post the actual values (and when debugging you
should never assume that the values are what you think they are), but two
points spring to mind:

1. innerHTML is a non-standard, MS-proprietary property which, although
supported as a convenience by other browsers, doesn't have a standard
defined behaviour;

2. When you read the innerHTML property you are getting the string after
it has been parsed by the browser, so although the string returned may be
semantically identical to the value that was stuffed in there, it's
possible that whitespace has been changed around (for example, duplicate
spaces could have been stripped) with the result that the two strings are,
in fact, different. Another possibility is that any HTML elements
contained in the element whose innerHTML property you are reading would be
returned with their tag names in upper case, but the initial value with
which you are comparing has tag names in lower case.

Try:

alert(stringFromAjax);
alert(stringFromInnerHTML);

(with the appropriate variables, obviously) and I bet you'll find the two
strings are different.

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/