Thanks Rob,
Because I am new to Javascript, I am not quite understand what is
the usage of the following if statement
The overall strategy is to test for things that are expected or
required so as to not cause errors. Where that strategy involves
testing expected built-in and host features, it is called feature
detection, you can read more here:
<URL:
http://www.jibbering.com/faq/faq_notes/not_browser_detect.html#bdFD
if (el.name &&
document.getElementById &&
(o = document.getElementById(el.name + '_err')))
The && (AND) operator can be used to replace a sequence of nested if
statements. The above is the same as:
if (el.name) {
if (document.getElementById) {
o = document.getElementById(el.name + '_err');
if (o) {
...
}
}
}
When the el.name will return true, and what does el.name check for.
It checks that el.name returns a value that converts to true in the
conditional part of the if statement, i.e. that the element has a
name.
And what is the meaning of document.getElementById and
That checks that the document object (which I've assumed exists) has a
property named getElementById and that it returns a value that
converts to true as above. It is a reasonable (though not thorough)
test that the host environment supports the W3C DOM 2 Core
document.getElementById method.
You can search the archives here for recent (last 3 months) threads on
feature detection to see how far this concept can be taken[1]. The
above is probably the simplest that is reasonable to use in this
example, there are much more thorough methods.
(o = document.getElementById(el.name + '_err')))
That calls document.getElementById(...) and assigns the result to o,
which is then used in the conditional part of the if statement to see
if it converts to true, i.e. it checks that o has been assigned a
reference to an element (hopefully the one we're after). If an
element with a matching ID was not found, o would be null and convert
to false.
1. Here's a link to get you started:
<URL:
http://groups.google.com.au/group/c...st&q=code+worth+recommending#4cbeb43d1c6ceecc