HighDots Forums  

User Agent Detection Logic

Javascript JavaScript language (comp.lang.javascript)


Discuss User Agent Detection Logic in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-21-2003 , 06:24 PM






"Fotios" <f_bass (AT) yahoo (DOT) com> writes:

Quote:
I have put together a flexible client-side user agent detector (written in
js). I thought that some of you may find it useful. Code is here:

http://fotios.cc/software/ua_detect.htm
My only real problem with this code, is the problem it tries to solve.

You are trying to make a white-list of allowed browsers. White-lists
are not a very good idea, since they require constant updating. A
black-list is better. If you give a warning for browsers that you know
are *not* compatible with your page, and you write code that works
with modern standards, then you will rarely have to update the list.
That is, give an unknown browser the benefit of the doubt.

You also base the detection solely on the navigator.userAgent string.
That is not a reliable method, as some browsers are known to fake that
string.

There are some minor stuff:

- You use parseInt with only one argument. (And which browser has the
length of an array as a string?)

- You use "new Array(elem1,elem2,...,elemn);", which doesn't seem to
initialize the array in Netscape 2.02 (nor does the length property
of the array work).

- You write <script language="JavaScript">, which is illegal in HTML 4+.
The type attribute is required.


Quote:
The detector requires javascript 1.0 to work. This translates to
netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
IE 2 didn't have scripting at all. That was introduced in IE 3.0b2,
IIRC. I don't think the script is compatible with Netscape 2.

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #2  
Old   
Richard Cornford
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-21-2003 , 07:20 PM






"Fotios" <f_bass (AT) yahoo (DOT) com> wrote

Quote:
I have put together a flexible client-side user agent detector (written
in js). I thought that some of you may find it useful. Code is here:

http://fotios.cc/software/ua_detect.htm

The detector requires javascript 1.0 to work. This translates to
netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
User agent detecting is unnecessary in almost all cases but of the
methods that have been attempted the use of the browser's
navigator.userAgent string is easily the most useless as very few
current browsers report consistent or accurate information in this
string. Often in order to avoid this type of clumsy and misguided
detection categorising them as "unknown" or "declined" when their
JavaScript and DOM support is entirely up to the requirements of the
majority of scripts and their authors can see no reason for them to be
excluded just because some script author only knows enough to recognise
7 browsers by name.

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

Richard.




Reply With Quote
  #3  
Old   
Fotios
 
Posts: n/a

Default User Agent Detection Logic - 09-21-2003 , 07:35 PM



Hi guys,

I have put together a flexible client-side user agent detector (written in
js). I thought that some of you may find it useful. Code is here:

http://fotios.cc/software/ua_detect.htm

The detector requires javascript 1.0 to work. This translates to netscape
2.0 and IE 3.0 (although maybe IE 2.0 also works with it)

Cheers,
Fotios

--
http://fotios.cc/






Reply With Quote
  #4  
Old   
Richard Cornford
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-21-2003 , 09:36 PM



"Fotios" <f_bass (AT) yahoo (DOT) com> wrote

Quote:
User agent detecting is unnecessary in almost all cases but of the
methods that have been attempted the use of the browser's
navigator.userAgent string is easily the most useless as very few
current browsers report consistent or accurate information in this
string. Often in order to avoid this type of clumsy and misguided
detection categorising them as "unknown" or "declined" when their
JavaScript and DOM support is entirely up to the requirements
of the majority of scripts and their authors can see no reason for
them to be excluded just because some script author only knows
enough to recognise 7 browsers by name.

I rarely read something as inacurate as what you have just written.
So you haven't bothered to read the comp.lang.javascript FAQ before
posting then:-

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

- as it says essentially the same thing. (Still, I don't expect you
would consider that a document that is subject the scrutiny and review
of all of the regular poster to this group as having anything accurate
to say on the subject of browser scripting.)

Quote:
What about the fact that most script that goes around does not run
properly in more than one or two browsers?
"most script that goes around" is written (or more often cut-n-pasted)
by people who do not really understand what they are doing and should
not be taken as an example of best (and in many cases not even
acceptable) practice.

Quote:
A detector is about "what is" not about "what ought to be".
So test "what is" not "what ought to be" which is what you are testing
by assuming that the navigator.userAgent string ought to be a
discriminating indicator of the type and version of a web browser. It is
not and has not been for quite some time now.

As it is your code will identify Konqueror 3 as IE, Netscape, Opera,
Konqueror and unknown depending on which of the userAgent string I
choose form the list of 20 odd provided in the drop-down in the
preferences. While the same script will be absolutely convinced that my
Palm OS web browser is IE 6, but a script that treats it as IE 6 will
fail horribly. To qualify as a "detector" a script should be expected to
produce discriminating results. The navigator.userAgent string just
cannot provide that.

But, as I said, it is almost never necessary to know the browser type or
version. Feature detection is the preferred strategy. A script author
should know what features a browser must support in order for a script
to work. Testing for the existence of those features prior to execution
allows a script to execute in any browser that supports them and if they
are not available it can cleanly and harmlessly exit. A strategy that is
only interested in "what is" available on the browser in question, but
without any interest in which browser that actually is. Also a strategy
that can work successfully in a completely unknown browser, exploiting
any browser up to its ability to support the script.

Richard.




Reply With Quote
  #5  
Old   
Fotios
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-21-2003 , 09:38 PM



Hi Lasse,

thanks for taking the time to help me (or us) improve this code!

Quote:
My only real problem with this code, is the problem it tries to solve.

You are trying to make a white-list of allowed browsers. White-lists
are not a very good idea, since they require constant updating. A
black-list is better. If you give a warning for browsers that you know
are *not* compatible with your page, and you write code that works
with modern standards, then you will rarely have to update the list.
I understand what you are trying to say but my experience from web
development says that if you want to play it safe you should only allow your
code to run on specific browsers - the browsers you have tested it with.
However, if you review the code you will see that is uses a list that is
both black and white; i.e. you can have both white listed and black listed
browsers (Black listing takes precedence)


Quote:
That is, give an unknown browser the benefit of the doubt.
If you check the code you will see there is an option just for that (i.e.
whether you want to allow an unknown browser to continue or not)


Quote:
You also base the detection solely on the navigator.userAgent string.
That is not a reliable method, as some browsers are known to fake that
string.
Well, let's say that I won't be interested in covering this case just yet.
AFAIK all major browsers have a default user agent string that is
distinctive in some way and the rest of them (not major) should as well.


Quote:
There are some minor stuff:

- You use parseInt with only one argument. (And which browser has the
length of an array as a string?)


- You use "new Array(elem1,elem2,...,elemn);", which doesn't seem to
initialize the array in Netscape 2.02 (nor does the length property
of the array work).
You are right on both of these two remarks. The first one was due to my
sloppy testing of NN 2.0; I was getting an error and I assumed this was due
to wrong typing of the length property of arrays but further testing I just
did shows that this property is simply undefined (thus the error).

And yes, this style of Array initialization does not seem to work with NN
2.0.

So, what I still need to make this work under NN 2.0 is:
- write a function that counts the length of an array in a way that is JS
1.0 compatible.
- initialize the array with arr[index] = value; type of statements

I should have the revised version soon.

BTW, there is a great old browser software archive here:
http://browsers.evolt.org/


Quote:
- You write <script language="JavaScript">, which is illegal in HTML 4+.
The type attribute is required.
Thanks for letting me know. I added it.


Quote:

The detector requires javascript 1.0 to work. This translates to
netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)

IE 2 didn't have scripting at all. That was introduced in IE 3.0b2,
Yeah, some crooked hypothesis making on my part there. That was Netscape's
golden era.

Later,
Fotios





Reply With Quote
  #6  
Old   
Fotios
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-21-2003 , 09:50 PM




Quote:
User agent detecting is unnecessary in almost all cases but of the
methods that have been attempted the use of the browser's
navigator.userAgent string is easily the most useless as very few
current browsers report consistent or accurate information in this
string. Often in order to avoid this type of clumsy and misguided
detection categorising them as "unknown" or "declined" when their
JavaScript and DOM support is entirely up to the requirements of the
majority of scripts and their authors can see no reason for them to be
excluded just because some script author only knows enough to recognise
7 browsers by name.
I rarely read something as inacurate as what you have just written.

What about the fact that most script that goes around does not run properly
in more than one or two browsers? A detector is about "what is" not about
"what ought to be".

F.






Reply With Quote
  #7  
Old   
Fotios
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-22-2003 , 12:23 AM




Quote:
So you haven't bothered to read the comp.lang.javascript FAQ before
posting then:-

URL: http://jibbering.com/faq/#FAQ4_26
I do not need to read the FAQ before or after I post.

Quote:
- as it says essentially the same thing. (Still, I don't expect you
would consider that a document that is subject the scrutiny and review
of all of the regular poster to this group as having anything accurate
to say on the subject of browser scripting.)
I don't know what to answer to you as I have not read the FAQ.

Quote:
What about the fact that most script that goes around does not run
properly in more than one or two browsers?

"most script that goes around" is written (or more often cut-n-pasted)
by people who do not really understand what they are doing and should
not be taken as an example of best (and in many cases not even
acceptable) practice.
Isn't it still a fact that most script that goes around is not
cross-browser?
I have also heard that no scripter really knows what he/she is doing. What
do you think about that?

Quote:
A detector is about "what is" not about "what ought to be".

So test "what is" not "what ought to be" which is what you are testing
by assuming that the navigator.userAgent string ought to be a
discriminating indicator of the type and version of a web browser. It is
not and has not been for quite some time now.
One can easily write a lynx-like user agent that supplies any user string
he/she damn pleases.
Why does that shatter the status of things? (The status being that all major
browsers have - and should have - distinctive default strings)

Quote:
As it is your code will identify Konqueror 3 as IE, Netscape, Opera
Konqueror and unknown depending on which of the userAgent string I
choose form the list of 20 odd provided in the drop-down in the
preferences.
Well, if you change the string you are on your own.

Quote:
While the same script will be absolutely convinced that my
Palm OS web browser is IE 6, but a script that treats it as IE 6 will
fail horribly. To qualify as a "detector" a script should be expected to
produce discriminating results. The navigator.userAgent string just
cannot provide that.
Only because you were naughty and you changed the string.
What is the motivation in coding for the miniscule percentage of users who
not only use virtually non-existent browsers but also play with their user
agent strings?

Quote:
But, as I said, it is almost never necessary to know the browser type or
version. Feature detection is the preferred strategy. A script author
should know what features a browser must support in order for a script
to work.
Testing for the existence of those features prior to execution
allows a script to execute in any browser that supports them and if they
are not available it can cleanly and harmlessly exit. A strategy that is
only interested in "what is" available on the browser in question, but
without any interest in which browser that actually is. Also a strategy
that can work successfully in a completely unknown browser, exploiting
any browser up to its ability to support the script.
1. Detecting a browser type is not necessarily for scripting purposes. For
instance, you may not like the way something renders in gecko.

2. I don't want to build a detector every time depending on what I want to
use.

3. A generic feature based browser detector seems to be much more work than
the one I posted. If you build one let me know and I will give it a try.

F.





Reply With Quote
  #8  
Old   
Ivo
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-22-2003 , 12:42 AM



"Fotios" <f_bass (AT) yahoo (DOT) com> wrote

Quote:
So you haven't bothered to read the comp.lang.javascript FAQ before
posting then:-

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

I do not need to read the FAQ before or after I post.
You won't be jailed, but it seriously harms both your insight and our
sympathy if you don't.

Quote:
I have also heard that no scripter really knows what he/she is doing. What
do you think about that?
No one can speak but for themselves, especially if they spend their time
behind computer screens. I for one haven't a clue.

Quote:
While the same script will be absolutely convinced that my
Palm OS web browser is IE 6, but a script that treats it as IE 6 will
fail horribly. To qualify as a "detector" a script should be expected to
produce discriminating results. The navigator.userAgent string just
cannot provide that.

Only because you were naughty and you changed the string.
What is the motivation in coding for the miniscule percentage of users who
not only use virtually non-existent browsers but also play with their user
agent strings?
I started reading this thread with a positive feeling, but this turns sour.
What is the movitation behind writing a script that scrutinizes something so
irrelevant? The bottom line is the navigator agent string thing can be
changed at will both by browser-makers and end-users. Mine is a Shakespeare
quote.
If I want my script to bark up the document.images tree, I will not test for
document.styleSheets or screen.availWidth or navigator.anyThing.

Regards,
Ivo




Reply With Quote
  #9  
Old   
HikksNotAtHome
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-22-2003 , 01:09 AM



In article <3f6e8b70$0$35681$1b62eedf (AT) news (DOT) wanadoo.nl>, "Ivo" <no (AT) thank (DOT) you>
writes:

<snip>

Quote:
I do not need to read the FAQ before or after I post.

You won't be jailed, but it seriously harms both your insight and our
sympathy if you don't.
Not to mention credibility. He multi-posted these same thoughts in the
microsoft.public.scripting.jscript NG
<snip>

Quote:
Only because you were naughty and you changed the string.
What is the motivation in coding for the miniscule percentage of users who
not only use virtually non-existent browsers but also play with their user
agent strings?

I started reading this thread with a positive feeling, but this turns sour.
You too?

<snip>

--
Randy


Reply With Quote
  #10  
Old   
Fotios
 
Posts: n/a

Default Re: User Agent Detection Logic - 09-22-2003 , 02:26 AM



Hi,

I have amended the code in a few places in order to be Netscape 2.0 (JS 1.0)
compatible:

* The JS1.0 parseFloat() function needed some help as it returns 0 (instead
of the NaN in later JS versions) when the initial char of the string is not
a digit.
There is now a helper function named: skipNonDigits()

* The array is now populated in a way that is JS 1.0 compatible

* I use substring() instead of substr(). This is because the former seems to
be working better in Netscape 2.0 although both are supported by JS 1.0

* In lack of the length property for arrays under JS 1.0 the length of the
browser array needs to be specified by the user along with the rest of the
parameters.

Revised code here:

http://fotios.cc/software/ua_detect.htm

Cheers,
Fotios




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.