Forum Moderators: open

Message Too Old, No Replies

Comparing two identical strings

... and JS thinks they're different

         

trillianjedi

1:29 pm on Jul 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Clearly I'm doing something wrong, but I can't get my head around this one. Wonder if anyone can point me in the right direction?

I have an object class with a property called lastString, which is init'd as follows:-

serverManager.lastString = "";

I have a JAVA applet on a page, which has a function I can call and should return a string to me, which is JSON (for later EVAL):-

var myApplet = document.getElementById('myApplet');
var rjson = myApplet.getTheJSONThing();

The idea of this is an optimisation. If the rjson string is the same as it was last time, I don't need to do anything with it (eg, I'm looking for changes):-

if (rjson != serverManager.lastString) {
updateTheUI();
serverManager.lastString = rjson;
} else {
doNothing();
}

The problem is, rjson never equals serverManager.lastString. They are always different as far as this conditional is concerned, even though when I alert both strings I can quite clearly see they are identical.

Any ideas on why the == is saying they are different, although when I display them they are the same?

Thanks!

coopster

5:57 pm on Jul 10, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alert the typeof the two and see if they are of the same type. You may need to actually convert to string for the comparison. Depends on what your function is returning.