471,316 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

basic javascript syntax question

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.
Jul 23 '05 #1
4 1586
On 19 Sep 2004 07:53:38 -0700, Ryan Gaffuri <rg******@cox.net> wrote:
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:

<URL:http://jibbering.com/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.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
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.

Jul 23 '05 #3
In article <1e**************************@posting.google.com >,
rg******@cox.net (Ryan Gaffuri) wrote:
var v = navigator.appVersion.toUpperCase()
if (1+v.indexOf('WIN98')
os = 'Win98';
else if (1+v.indexOf('WINNT')
os = 'WinnT';
These if statements are not valid. An ending ) is needed.
. However,
'indexof' is the kind of function used with an array that tells you
what index a value is at. indexOf finds the starting position of a query string inside of the
target string.
Why is ther '1+' and where is the '='. I
dont see an if check.
JavaScript uses the c style syntax.

This means
1) the array index starts with 0
2) the string index starts with 0
3) the = always means an assignment even inside of an if statement
4) the == test for equality
5) the if statement has a short cut comparison built in. if (number) is
like if (number != 0) or if (boolean) like if (boolean == true) or etc.
if (1+v.indexOf('WIN98'))


since indexOf return a number from zero on up when the query string is
found and -1 when the query string isn't found. The author added 1 to
the found value to make a number comparison take the true path in the if
statement when the string is found and the false path when the string
isn't found.

I'd have written
if (v.indexOf('WIN98') >= 0)

Some c programmers like to be "clever" and do things in a non-obvious
way. They may do this in the name of performance, but working code
performs better than non-working code.

I find c style syntax more cryptic than I would like. Fortunately,
Javascript has eliminate the worst of the problems.

You may want to pick up a book on Javascript. Consider javascript: The
Definitive Guide by David Flanagan. You may also want to buy one of the
other books on Javascript.

if (1+v.indexOf('WIN98'))

The technique of checking for operating system the browser is running in
has proven to be unreliable. Many browsers fib on what the running
environment is to let them into sites.

A better method is called feature detection. That is if you want to use
the function getElementById, you do a test for it before using it. For
example:
var myValue;
if (document.getElementById )
{ myValue = document.getElementById(theID).value; }

Robert
Jul 23 '05 #4
Ryan Gaffuri wrote:
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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Alex Fitzpatrick | last post: by
1 post views Thread by James | last post: by
6 posts views Thread by PulsarSL | last post: by
43 posts views Thread by Bill H | last post: by
reply views Thread by rosydwin | last post: by

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.