473,396 Members | 1,608 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,396 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 1698
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex Fitzpatrick | last post by:
Just by way of introduction, I'm currently the principal developer and maintainer of the a JavaScript editor plug-in for Eclipse. https://sourceforge.net/projects/jseditor/ The plug-in as it...
1
by: James | last post by:
I am looking to add the domain name into the login url to access webmail via a link. Example: location.href = "https://" + "domain_name%5C" + LOGINNAME + ":" + password + "@test.test1.com" ...
2
by: Eli | last post by:
Hi gurus, My question is about exchanging data on a basic HTML page between a JavaScript piece of code and a Java client-side <APPLET> residing on the same webpage. To demonstrate the idea of...
6
by: PulsarSL | last post by:
Hey, I used to know javascript, but it's been years since I've used it. I'm trying to relearn it, but I'm stuck already. Can anyone tell me why this does nothing when I load the page? ...
6
by: John Bailo | last post by:
http://www.informationweek.com/software/showArticle.jhtml?articleID=196600515 Developers Embrace Java, Drop Visual Basic "Developers have abandoned Microsoft's Visual Basic in droves during...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
43
by: Bill H | last post by:
25 years ago every computer came with some form of Basic interpreter so you could use yoru computer without having to buy more software. Is Javascript (teamed with HTML) set to become the new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.