By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

Truth in javascript

P: n/a
Hi everyone,

I'm a bit confused about some of the expressions used in conditional
statements.

One example:

for( i = 0; i < document.all.tags( elmID ).length; i++ )
{
obj = document.all.tags( elmID )[i];
if( !obj)
{
....
}
}

is this statement testing for the existence of the tags property? What
will the values stored in obj be if tags is/isn't supported? Will the
javascript continue to execute? (in my experience, FF shows an error
message in the javascript console saying that the property doesn't
exist).

The code above is included in an ' if(ie)' block, where 'ie =
document.all'. Having said that (and assuming that the initial code
listing is indeed testing the existence for the tags property), how can
the 'for' loop ever be entered if the tags property does exist (seeing
its used in the exit condition for the for loop)

Another unrelated question:

One line of code writes:

if (groupdiv.children[i].value =="undefined")

Is this testing for the existence of the value property, or is it
testing to see if the value property is equal to 'undefined'.

Is this any different to writing:

if (groupdiv.children[i].value ==undefined)

(that is, without the speechmarks) - both statements appear in the same
body of code.

TIA for any help!

Taras

Jan 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Taras_96" <ta******@gmail.com> writes:
I'm a bit confused about some of the expressions used in conditional
statements. .... obj = document.all.tags( elmID )[i];
(notice that "document.all" is part of the proprietary IE DOM, and
not a standard. Some browsers are likely to not support it.)
if( !obj) ....
is this statement testing for the existence of the tags property?
It checks that the value of the variable "obj" is not null (or, in
this case unlikely, one of the values: undefined, false, "", 0 or NaN).

I can't see why they do it, though.

They first make sure that "i" is in the range 0..length-1 of a
collection, and then pick out the i'th element of that collection.
It shouldn't be able to be null.
Will the javascript continue to execute? (in my experience, FF shows
an error message in the javascript console saying that the property
doesn't exist).
If you check the line number, it might be already at the first reference
to document.all.tags (recent Firefoxen have supported document.all, I
don't know whether they support document.all.tags).
The code above is included in an ' if(ie)' block, where 'ie =
document.all'.
That would disqualify Firefox. Their document.all object is a host
object that converts to a false value in a boolean context, exactly
to avoid being detected as IE.
One line of code writes:

if (groupdiv.children[i].value =="undefined")

Is this testing for the existence of the value property, or is it
testing to see if the value property is equal to 'undefined'.
It's testing that the value property exists and is equal to the string
"undefined". Even after type conversion, the value undefined will not
be equal to that string.
Is this any different to writing:

if (groupdiv.children[i].value ==undefined)
Yes.
Try executing
alert("undefined"==undefined);
(that is, without the speechmarks) - both statements appear in the same
body of code.


Ick.

What was meant might be:
if (typeof groupdiv.children[i].value == "undefined")

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 30 '06 #2

P: n/a
Lasse Reichstein Nielsen wrote:
"Taras_96" <ta******@gmail.com> writes:
I'm a bit confused about some of the expressions used in conditional
statements.

...
obj = document.all.tags( elmID )[i];


(notice that "document.all" is part of the proprietary IE DOM, and
not a standard. Some browsers are likely to not support it.)


I think since IE-based browsers, Gecko-based browsers and Opera-based
browsers now featuring document.all, it is justified to say that there
are some browsers that do not support it and many browsers that are
likely not to support it _properly_, which is even worse. There are
widespread DOMs out there that (for whatever reason) do expose this
property but do not implement it as it is implemented in the IE DOM,
which is even more error-prone.
PointedEars
Jan 30 '06 #3

P: n/a
Cheers or the replies. It's been a hectic couple of weeks and I almost
forgot I had written this message!

Feb 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.