用户代理检测逻辑 [英] User Agent Detection Logic

查看:56
本文介绍了用户代理检测逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经整理了一个灵活的客户端用户代理检测器(用

js编写)。我以为你们中的一些人可能觉得它很有用。代码在这里:

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


探测器需要javascript 1.0才能工作。这转换为netscape

2.0和IE 3.0(虽然IE 2.0也可以使用它)


干杯,

Fotios


-
http://fotios.cc/

解决方案

" Fotios" <˚F_ **** @ yahoo.com>写道:

我已经整理了一个灵活的客户端用户代理检测器(用
js编写)。我以为你们中的一些人可能觉得它很有用。代码在这里:

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

这个代码我唯一真正的问题是它试图解决的问题。


你正试图制作允许浏览器的白名单。白名单

不是一个好主意,因为它们需要不断更新。

黑名单更好。如果您对浏览器发出警告,您知道

与您的页面不兼容,并且您编写的代码符合现代标准,那么您很少会更新列表。

也就是说,给一个未知浏览器带来疑问的好处。


你也只能在navigator.userAgent字符串上进行检测。 。

这不是一个可靠的方法,因为有些浏览器会伪造

字符串。


有一些小的东西:


- 你只使用一个参数使用parseInt。 (以及哪个浏览器的数组长度为

作为字符串?)


- 你使用new Array(elem1,elem2,... ,elemn);",似乎没有

在Netscape 2.02中初始化数组(数组的长度属性

也不起作用)。 />

- 您编写< script language =" JavaScript">,这在HTML 4+中是非法的。

类型属性是必需的。


探测器需要javascript 1.0才能工作。这转换为
netscape 2.0和IE 3.0(虽然IE 2.0也可以使用它)




IE 2根本就没有脚本。这是在IE 3.0b2中引入的,

IIRC。我不认为该剧本与Netscape 2兼容。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

Art D''HTML:< URL:http://www.infimum .dk / HTML / randomArtSplit.html>

''没有判断的信仰只会降低精神神圣。''


" Fotios" <˚F_ **** @ yahoo.com>在消息中写道

新闻:3f ********************* @ news.gradwell.net ...

我已经整理了一个灵活的客户端用户代理检测器(用js编写)。我以为你们中的一些人可能觉得它很有用。代码在这里:

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

检测器需要javascript 1.0才能工作。这转换为
netscape 2.0和IE 3.0(虽然IE 2.0也可以使用它)




几乎在所有情况下都不需要用户代理检测

尝试使用浏览器的方法

navigator.userAgent字符串很容易就是最无用的因为很少

当前浏览器在此

字符串中报告一致或准确的信息。通常为了避免这种笨拙和被误导的检测,将它们归类为未知。或拒绝当他们的

JavaScript和DOM支持完全符合

大多数脚本的要求时,他们的作者可以认为没有理由让他们成为
$ b排除$ b只是因为某些脚本作者只知道足以识别

7浏览器的名称。


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


Richard。


Hi Lasse,


感谢您抽出宝贵时间帮助我(或我们)改进此代码!


这个代码我唯一真正的问题是它试图解决的问题。

你正试图制作一个允许的白名单浏览器。白名单
不是一个好主意,因为它们需要不断更新。
黑名单更好。如果您对浏览器发出警告,您知道它们与您的页面不兼容,并且您编写了符合现代标准的代码,那么您很少需要更新列表。


我明白你要说的是什么,但是我从网上获得的经验
开发说如果你想安全地玩,你应该只允许你的/>
代码在特定浏览器上运行 - 您测试过的浏览器。

但是,如果您查看代码,您会看到使用的是一个列表

黑白两色;即你可以同时列出白名单和黑名单

浏览器(黑名单优先)


也就是说,给未知浏览器带来疑问。


如果您检查代码,您会看到有一个选项(即

是否要允许未知浏览器继续)


你也只能在navigator.userAgent字符串上进行检测。
这不是一种可靠的方法,因为有些浏览器会伪造
字符串。


好​​吧,让我们说我还没有兴趣报道此案例。

AFAIK所有主流浏览器都有默认用户代理字符串

在某种程度上是独特的,其余的(不是主要的)也应该。


有一些小的东西:

- 您只使用一个参数使用parseInt。 (以及哪个浏览器的数组长度为字符串?)

- 你使用new Array(elem1,elem2,...,elemn);,似乎没有在Netscape 2.02中初始化数组(数组的长度属性也不起作用)。


你对这两个评论都是正确的。第一个是由于我对NN 2.0的粗略测试;我得到一个错误,我认为这是由于错误输入数组的长度属性而导致错误输入,但进一步测试我只是

确实显示此属性是未定义的(因此错误)。


是的,这种阵列初始化似乎不适用于NN

2.0。


所以,我仍需要在NN 2.0下完成这项工作的是:

- 编写一个以JS的方式计算数组长度的函数

1.0兼容。

- 使用arr [index] = value初始化数组;类型的陈述


我很快就会有修改后的版本。


BTW,这里有一个很棒的旧浏览器软件存档:
http://browsers.evolt.org/


- 您编写< script language =" JavaScript">,这在HTML 4+中是非法的。
type属性是必需的。


感谢您告诉我。我添加了它。


检测器需要javascript 1.0才能工作。这转换为
netscape 2.0和IE 3.0(尽管IE 2.0也可以使用它)



IE 2根本就没有脚本。这是在IE 3.0b2中引入的,




是的,在我那里做了一些歪曲的假设。那是Netscape的

黄金时代。


后来,

Fotios


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/

解决方案

"Fotios" <f_****@yahoo.com> writes:

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.

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 - lr*@hotpop.com
Art D''HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''Faith without judgement merely degrades the spirit divine.''


"Fotios" <f_****@yahoo.com> wrote in message
news:3f*********************@news.gradwell.net...

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.


Hi Lasse,

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


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)

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)


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.


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/


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


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


这篇关于用户代理检测逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆