Subj : Re: Getting string objects to compare equal To : Steve Evans From : Brendan Eich Date : Tue Sep 20 2005 02:00 pm Steve Evans wrote: > Here's a snippet of alas-all-too-common script from a website: > > if (theForm.frmEmail.value != theForm.frmEmailConf.value) { > errAlert("The E-mail addresses entered do not match") > return false > } > > This is comparing two string *objects*, No, value for text inputs is of string type, not object. > And yet it works in current versions of Firefox and Opera > and IE, but not in my embedded SpiderMonkey environment, > which fails "correctly", as per the JS spec. Your embedding has a bug. You are misreading the spec, or really, reading the wrong spec (it's the DOM spec, not the JS, i.e. ECMA-262 Edition 3 spec, that says what type the value property must be). > For a while I thought that maybe Firefox interned the values > of the form controls using JS_InternString(), so that the > frmEmailConf.value really does use the same JSString as > frmEmail.value. More confusion: JSString is not JSObject, so I don't know why you were talking about objects above. Anyway, SpiderMonkey compares string values (tagged JSString pointers) by comparing their lengths and, if lengths are equal, all of their characters. So I'm still not sure what bug you have here. > However, a grep through the source reveals > that JS_InternString() is only used for read-only constant > strings that will really benefit from re-use: attribute > names rather than attribute values. Intern'ing is for well-known names, not random user inputs. Any sharing benefit from my typing the same string into two forms where the values have similar lifetimes is minimal. Don't intern unknown or arbitrary strings. > So how do I emulate Firefox's forgiving behaviour? It's not Firefox's alone, it is any standards-conforming browser's. /be .