473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Truth in javascript

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
3 1401
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lkrubner | last post by:
I've got a site on a hosted virtual server. I'd like to know the truth of what is going on, but I can't get past my browsers cache. There is some kind of error in my code that occurs on a given...
4
by: James Stroud | last post by:
Hello All, I'm curious, in py0 | (1 == 1) 1 pyFalse | (1 == 1) True What is the logic of the former expression not evaluating to True (or
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
2
by: lemnitzer | last post by:
TV report: all 15 British sailors confess to illegally entering Iranian Waters Here are the links: http://news.google.com/news?hl=en&ned=us&q=all+15&btnG=Search+News...
6
by: natkw1 | last post by:
Hi all, I'm new to C and kind of confused on this. I've had a look around this group for suggestions but still not sure why the warning is occurring. What I've done is this <code snip> void...
4
by: laelahaellallah5 | last post by:
Explore & discover & be convinced that ISLAM is the truth! ... Please Visit: http://www.beconvinced.com
1
by: Rohanrajs82 | last post by:
Hi, Can anybody help me? I have to construct a truth table of 97 columns. I have tester channels from 1 to 97. I have to make a truth table with all the possible combinations. Also, I have some...
6
by: ambanks04 | last post by:
ok taking computer science class anybody know what to do i am not computer literate CoSc 111.003 Spring 2008 Assignment 4 1) Each student shall design, develop, code, test and submit an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.