Click to See Complete Forum and Search --> : Can anyone explain what this line of code does, please?


Padrill
04-25-2003, 06:44 AM
I'm studying the External JS script from javascript.internet.com/forms

And this line is confusing me:

if (!(x = document[strName]) && document.all) x = document.all[strName];

It looks as if it tries to assign document[strName] to x (if there is an element with name=strName and there are elements in the document) otherwise it assigns document.all[strName] to x

Why doesn't it do x=document.all[strName] in the first place?

I suspect that it has to do with browser compatibility but can anyone confirm that please?

Also, does anyone know any good advanced JavaScript Tutorial-Reference - I've had enough with the ones that describe variables and loops.

Thanx.

gil davis
04-25-2003, 07:23 AM
Originally posted by Padrill
I suspect that it has to do with browser compatibility but can anyone confirm that please?You suspect correctly. Another obtuse way of doing it would bex = (document[strName]) ? document[strName] : document.all[strName];Not exactly friendly. A more clear coding style would beif (document[strName])
{x = document[strName];}
else
{if (document.all)
{x = document.all[strName];}
else
{x = null;} // this is just for completeness
}

Padrill
04-25-2003, 09:00 AM
x = (document[strName]) ? document[strName] : document.all[strName];
How didn't I thought of this earlier???

Thanx.

if (document.[strName])

Did you intend the . between document and [ or is it a typo?

gil davis
04-25-2003, 09:11 AM
Did you intend the . between document and [ or is it a typo?No, it's a typo. My bad.