473,881 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What does this mean

I am learning Javascript; and most books only give you partial
definitions for the functions they show you. Here is a line of code
from a browser sniffing function:

var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;

What does the -1 stand for.

Excuse me for such a basic question, but I can't find the answer
anyway.

Thanks
Jul 23 '05 #1
8 1724
On 17 Sep 2004 09:06:39 -0700, Kim Forbes wrote:
I am learning Javascript; and most books only give you partial
definitions for the functions they show you. Here is a line of code
from a browser sniffing function:
Browser sniffing? If the book burns, it may
be useful to keep you warm for a few minutes,
otherwise it has no (good) use.

Search the group for 'feature detection'. Once
you have the hang of that, you will no longer
need to worry about what browser your visitors
are using.
var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;

What does the -1 stand for.


In the string 'AWindowsPC', 'Win' occurs..
^
0123456789
...at index 1.

In the string 'TheWindow', 'Win' occurs
^
0123456789
...at index 3.

...but in the string "ThisIsAnAppleM acintosh",
'Win' occurs ..nowhere. The function returns
-1 to indicate that 'Win' was *not* contained
in the string "ThisIsAnAppleM acintosh".

( ..but use feature detection, browser sniffing
is pointless, stupid, and leads to fragile code. )

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
Jul 23 '05 #2
On 17 Sep 2004 09:06:39 -0700, Kim Forbes <ki********@tch .harvard.edu>
wrote:
I am learning Javascript; and most books only give you partial
definitions for the functions they show you. Here is a line of code from
a browser sniffing function:

var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;
This could be written as

var isWin = (navigator.plat form.indexOf('W in') != -1);

The conditional statement is superfluous as the comparison already
evaluates to a boolean. However, you should avoid this sort of thing (see
below).
What does the -1 stand for.


The String.prototyp e.indexOf method returns the index of the first
occurance of the supplied string. As in most circumstances, indicies start
at zero (0), so minus one (-1) is used to signify that the substring
couldn't be found.

It's important that you ignore anything regarding browser sniffing. It's a
flawed technique that often results in scripts that break with unknown
user agents, or those that spoof themselves as other browsers. Instead,
you should learn about feature detection. See:

<URL:http://jibbering.com/faq/#FAQ4_26>

and its links.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Kim Forbes wrote:
I am learning Javascript; and most books only give you partial
definitions for the functions they show you. Here is a line of code
from a browser sniffing function:

var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;

What does the -1 stand for.

Excuse me for such a basic question, but I can't find the answer
anyway.

Thanks


You will only learn if you ask.

isWin is a boolean with values only true or false.

The above is a short way to write an if, else statement.

The -1 means that the String "Win" is not present in the String returned
by navigator.platf orm If it was the value would have been the first
occurence of the "W" in "Win" starting to count from 0 in the String
returned by navigator.platf orm. This short statement tests if the user
uses any variant of Windows, in itself a bad thing to do. The
intelligent user uses Linux, or maybe even a Mac.

If you want to I can rewrite that into the mentioned if, else statement
which is more easily understandable.

Chris

Jul 23 '05 #4
Antonie C Malan Snr <ma*******@optu snet.com.au> wrote in message news:<41******* *************** *@news.optusnet .com.au>...
Kim Forbes wrote:
I am learning Javascript; and most books only give you partial
definitions for the functions they show you. Here is a line of code
from a browser sniffing function:

var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;

What does the -1 stand for.

Excuse me for such a basic question, but I can't find the answer
anyway.

Thanks


You will only learn if you ask.

isWin is a boolean with values only true or false.

The above is a short way to write an if, else statement.

The -1 means that the String "Win" is not present in the String returned
by navigator.platf orm If it was the value would have been the first
occurence of the "W" in "Win" starting to count from 0 in the String
returned by navigator.platf orm. This short statement tests if the user
uses any variant of Windows, in itself a bad thing to do. The
intelligent user uses Linux, or maybe even a Mac.

If you want to I can rewrite that into the mentioned if, else statement
which is more easily understandable.

Chris


Thanks for the answer. Actually, what I would like is a good reference
book where I can learn Javascript inside out. I took a class about a
year ago, but it didn't help me like I wanted. I found many mistakes
in the textbook, and it wasn't as comprehensive as I wanted. What I
want to know is what every line in the code I'm writing means, and why
am I writing that way. As it is now, I can generally write enough code
to get it to do what I want; but I'm not sure why it works like it
does. I'm just parrotting what I learned.
Jul 23 '05 #5
Andrew Thompson <Se********@www .invalid> wrote in message news:<14******* *************** ********@40tude .net>...
On 17 Sep 2004 09:06:39 -0700, Kim Forbes wrote:
I am learning Javascript; and most books only give you partial
definitions for the functions they show you. Here is a line of code
from a browser sniffing function:


Browser sniffing? If the book burns, it may
be useful to keep you warm for a few minutes,
otherwise it has no (good) use.

Search the group for 'feature detection'. Once
you have the hang of that, you will no longer
need to worry about what browser your visitors
are using.
var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;

What does the -1 stand for.


In the string 'AWindowsPC', 'Win' occurs..
^
0123456789
..at index 1.

In the string 'TheWindow', 'Win' occurs
^
0123456789
..at index 3.

..but in the string "ThisIsAnAppleM acintosh",
'Win' occurs ..nowhere. The function returns
-1 to indicate that 'Win' was *not* contained
in the string "ThisIsAnAppleM acintosh".

( ..but use feature detection, browser sniffing
is pointless, stupid, and leads to fragile code. )

HTH


Thanks so much. That was exactly what I wanted to know. I started
checking out feature detection, it seems that once I figure it out, it
will be a lot easier to write and implement.

Thanks again.
Jul 23 '05 #6
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message news:<opsehcj7h nx13kvk@atlanti s>...
On 17 Sep 2004 09:06:39 -0700, Kim Forbes <ki********@tch .harvard.edu>
wrote:
I am learning Javascript; and most books only give you partial
definitions for the functions they show you. Here is a line of code from
a browser sniffing function:

var isWin = (navigator.plat form.indexOf("W in") !=-1) ? true:false;


This could be written as

var isWin = (navigator.plat form.indexOf('W in') != -1);

The conditional statement is superfluous as the comparison already
evaluates to a boolean. However, you should avoid this sort of thing (see
below).
What does the -1 stand for.


The String.prototyp e.indexOf method returns the index of the first
occurance of the supplied string. As in most circumstances, indicies start
at zero (0), so minus one (-1) is used to signify that the substring
couldn't be found.

It's important that you ignore anything regarding browser sniffing. It's a
flawed technique that often results in scripts that break with unknown
user agents, or those that spoof themselves as other browsers. Instead,
you should learn about feature detection. See:

<URL:http://jibbering.com/faq/#FAQ4_26>

and its links.

Mike


Mike,
I've bookmarked the URL. Thanks.
Jul 23 '05 #7
In article <de************ **************@ posting.google. com>,
ki********@tch. harvard.edu (Kim Forbes) wrote:
Thanks for the answer. Actually, what I would like is a good reference
book where I can learn Javascript inside out.


This group recommends javascript: The Definitive Guide by David
Flanagan.

Robert
Jul 23 '05 #8
JRS: In article <rc************ *************** **@news2.west.e arthlink.n
et>, dated Mon, 20 Sep 2004 19:08:11, seen in news:comp.lang. javascript,
Robert <rc*******@my-deja.com> posted :
In article <de************ **************@ posting.google. com>,
ki********@tch. harvard.edu (Kim Forbes) wrote:
Thanks for the answer. Actually, what I would like is a good reference
book where I can learn Javascript inside out.


This group recommends javascript: The Definitive Guide by David
Flanagan.


AIUI, this group merely considers it to be the best available book.
That is not the same as saying that it will enable one to "learn
Javascript inside out". For that, one should read ECMA-262, and make
all possible deductions from it. One will, also, need to learn about
DOMs.
Learning Javascript inside out can only be possible using multiple
sources of information, and much practice.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
14927
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
3
29692
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification says: "The 'visibility' property takes the value 'collapse' for row, row group, column, and column group elements. This value causes the entire row or column to be removed from the display, and the space normally taken up by the row or column to...
86
7830
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that: http://www.clagnut.com/blog/348/ I hope that's going to be a good discussion! Michael
44
4309
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there must be many know the answer here. thanks
2
10225
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table . GO
121
10255
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
51
4583
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
1
8750
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." What exactly does this mean? Does this mean that if I call a shared method from 2 different threads, nothing whacky will happen? Also when it says that instance members...
13
5071
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
550
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you change the & for int?
0
9776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.