basic javascript syntax question

R

Ryan Gaffuri

I know that this works. I just don't get the syntax.


I know its checking the OS. just not sure how it works.

var v = navigator.appVersion.toUpperCase()
if (1+v.indexOf('WIN98')
os = 'Win98';
else if (1+v.indexOf('WINNT')
os = 'WinnT';

etc....

ok I know that v is set to whatever the value of the OS is. However,
'indexof' is the kind of function used with an array that tells you
what index a value is at. Why is ther '1+' and where is the '='. I
dont see an if check.
 
L

Lee

Ryan Gaffuri said:
I know that this works. I just don't get the syntax.


I know its checking the OS. just not sure how it works.

var v = navigator.appVersion.toUpperCase()
if (1+v.indexOf('WIN98')
os = 'Win98';
else if (1+v.indexOf('WINNT')
os = 'WinnT';

etc....

ok I know that v is set to whatever the value of the OS is. However,
'indexof' is the kind of function used with an array that tells you
what index a value is at. Why is ther '1+' and where is the '='. I
dont see an if check.

The String.indexOf() method returns the position in the String
at which the argument is found. If the argument is not found in
the string at all, it returns -1.

When you add 1 to that returned value, the result will be zero
if the string is not found, or some positive number if it has
been found.

The "if" statement doesn't need an "==". It just needs an
expression that can be evaluated as true or false. If you give
it a number, it will consider it to be false if it is zero,
and true if it is any other number.
 
M

Michael Winter

I know that this works. I just don't get the syntax.


I know its checking the OS. just not sure how it works.

The first thing to mention is that browser-sniffing code should be
avoided. It results in fragile code that is unreliable. Furthermore, it is
based on information that the user agent chooses to provide. This is not
always the same as the true information. Many lesser-known browsers do
this because clueless authors screen their sites for certain browsers and
reject all the others.

If you need to determine the capabilities of a browser, use feature
detection. That, and other information is available in the group FAQ:

var v = navigator.appVersion.toUpperCase()
if (1+v.indexOf('WIN98')
os = 'Win98';
else if (1+v.indexOf('WINNT')
os = 'WinnT';

Ignoring the syntax errors[1], this code is quite simple. The if statement
evaluates the expression within parentheses as a boolean (true/false). The
boolean equivalent of a value depends on the type.

Type False True

Boolean false true
Number Zero (0) Any number except zero (including
negative values)
String Empty string Any string with a length >= 1
Object Null reference Any object reference
Null Always false
Undefined Always false

The indexOf method returns the position of a given string within another
string. As in the majority of cases, indicies start from zero, so -1 is
used to indicate that the substring couldn't be found.

If you apply the "+ 1" knowing the above, you can see that if the strings
'WIN98' and 'WINNT' can't be found, the if statement will evaluate (1
+ -1) which is false (0). If the strings can be found, indexOf will return
a number >= 0, which after adding 1 will always evaluate as true.

[snip]

Hope that helps,
Mike


[1] The if statements are missing closing parentheses. The code should
look something like:

if (1 + v.indexOf('WIN98')) {
os = 'Win98';
} else if (1 + v.indexOf('WINNT')) {
os = 'WinnT';
}

The braces in this instance are a style preference, but a good idea in the
event that the single statements become blocks.
 
G

Grant Wagner

Ryan said:
I know that this works. I just don't get the syntax.

I know its checking the OS. just not sure how it works.

var v = navigator.appVersion.toUpperCase()
if (1+v.indexOf('WIN98')
os = 'Win98';
else if (1+v.indexOf('WINNT')
os = 'WinnT';

etc....

ok I know that v is set to whatever the value of the OS is.

Actually, v is set to the value of navigator.appVersion.toUpperCase(),
which may or may not be the version of the browser your JavaScript is
running in, and may or may not contain the operating system being used.
Here are my values of navigator.appVersion.toUpperCase():

IE6SP1: 4.0 (COMPATIBLE; MSIE 6.0; WINDOWS NT 5.1; .NET CLR 1.1.4322)
Firefox 1.0PR: 5.0 (WINDOWS; )
Mozilla 1.7.3: 5.0 (WINDOWS; EN-US)
Opera 7.54 (set to "Identify as MSIE 6.0"): 4.0 (COMPATIBLE; MSIE 6.0;
WINDOWS NT 5.1)
Opera 6.05 (set to "Identify as MSIE 5.0"): 4.0 (COMPATIBLE; MSIE 5.0;
WINDOWS XP)
Netscape 4.78: 4.78 [EN]C-CCK-MCD 20011025 (WINDOWS NT 5.0; U) (it's a
custom build install using Netscape's CCK)

This is by no means a complete list of browsers in use, just a sampling
to demonstrate the variety of responses you can obtain from
navigator.appVersion. In each tested browser, the tests being done for
WIN98 and WINNT will not match anything in
navigator.appVersion.toUpperCase().

navigator.platform.toUpperCase() reports "WIN32" for each of the
browsers listed above (when running on Windows)

navigator.appCodeName.toUpperCase() reports "MOZILLA" for each of the
browsers listed above

navigator.userAgent.toUpperCase() provides slightly more information for
a couple of browsers:
Firefox 1.0PR: MOZILLA/5.0 (WINDOWS; U; WINDOWS NT 5.1; RV:1.7.3)
GECKO/20040913 FIREFOX/0.10
Mozilla 1.7.3: MOZILLA/5.0 (WINDOWS; U; WINDOWS NT 5.1; EN-US; RV:1.7.3)
GECKO/20040910

But navigator.userAgent is highly unreliable since it can be one of 5
values in Opera, and practically anything in Firefox/Mozilla.

In general, any information obtained by any property of the navigator
object is of little use on the public Internet. It may be of slightly
more value in the controlled environment of a corporate Intranet, where
you can be assured that a particular appVersion or userAgent actually
means the user is using a particular Windows version or
navigator.language means that the corporate user is in a particular part
of the world.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top