为什么浏览器嗅探不是推荐的做法? [英] Why is browser sniffing not a recommended practice?

查看:22
本文介绍了为什么浏览器嗅探不是推荐的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你到处都能听到:使用 javascript 嗅探用户代理字符串来检测浏览器版本是一件非常糟糕的事情.最新版本的 jQuery 现在已经弃用了它的 $.browser 对象来代替 $.support.但是,如果存在仅影响 IE 而不是其他浏览器的错误或问题,我该怎么办,我不知道为什么?

You hear it all over the place: using javascript to sniff the user agent string to detect browser versions is a Very Bad Thing. The latest version of jQuery has now deprecated its $.browser object in place of $.support. But what should I do if there's a bug or problem which is only affecting IE and not the other browsers, and I'm not sure why?

在我的例子中,一些 jQuery 代码使工具提示出现和消失,并带有鼠标悬停和鼠标移开的动画.在 Internet Explorer 中,它看起来很糟糕,而且很紧张,工具提示 div 在隐藏之前变成了一个非常大的尺寸,如果您将鼠标放在一堆带有提示的项目上,它真的会杀死浏览器.我不知道 IE 不支持"我应该针对哪些特定功能进行测试,因此仅嗅探 IE 并使用不同的方法要容易得多.我可以/应该做什么?

In my case, some jQuery code makes a tooltip appear and disappear with an animation on mouseover and mouseout. In Internet Explorer, it looks awful, and jittery, with the tooltip div changing to a really large size before hiding, and if you run your mouse over a heap of items with the tip it really kills the browser. I have no idea what particular feature IE doesn't "support" that I should be testing against, so it's much easier to just sniff for IE and use a different method. What could/should I do instead?

推荐答案

因为仅仅嗅探用户代理(jquery 为填充 $.browser 对象所做的)并不能告诉您全部真相.

Because just sniffing the user agent (which is what jquery does to populate the $.browser object) doesn't tell you the whole truth.

用户代理字符串可以在许多浏览器中轻松更改,因此,例如,如果您对似乎使用 IE 的每个人禁用某些在 IE 中不起作用的功能,您可能会意外禁用来自某些未来浏览器或出于某种原因(例如绕过基于浏览器嗅探的限制)的用户的那些功能,假装在使用 IE.

The user agent string can be easily changed in many browsers, so if you for example disable some features that don't work in IE from everybody who seems to be using IE, you might accidentally disable those features from some future browsers or users who just, for some reason (like for example to get around limitations based on browser sniffing), pretend to be using IE.

这似乎不是什么大问题,但它仍然是不好的做法.

This might not seem too big of a problem, but it is still bad practice.

是的,我也是 IE 嗅探器.我用

And yes, I am a IE sniffer too. I use

$.browser.msie && document.all

只是为了确定.

这篇关于为什么浏览器嗅探不是推荐的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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