![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
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) |
#2
| |||
| |||
|
|
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) |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
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". |
#5
| ||||||
| ||||||
|
|
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. |
| 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, |
#6
| |||
| |||
|
|
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. |
#7
| |||||||
| |||||||
|
|
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.) |
|
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. |
|
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. |
#8
| |||
| |||
|
|
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. |
|
I have also heard that no scripter really knows what he/she is doing. What do you think about that? |
|
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? |
#9
| |||
| |||
|
|
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. |
|
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. |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |