Browser sniffing

断了今生、忘了曾经 提交于 2019-12-04 09:55:00

Take a look at jQuery.browser: http://api.jquery.com/jQuery.browser/

The $.browser property provides information about the web browser that is accessing the page, as reported by the browser itself. It contains flags for each of the four most prevalent browser classes (Internet Explorer, Mozilla, Webkit, and Opera) as well as version information.

Available flags are:

webkit (as of jQuery 1.4) safari (deprecated) opera msie mozilla This property is available immediately. It is therefore safe to use it to determine whether or not to call $(document).ready(). The $.browser property is deprecated in jQuery 1.3, and its functionality may be moved to a team-supported plugin in a future release of jQuery.

Because $.browser uses navigator.userAgent to determine the platform, it is vulnerable to spoofing by the user or misrepresentation by the browser itself. It is always best to avoid browser-specific code entirely where possible. The $.support property is available for detection of support for particular features rather than relying on $.browser.

Don't worry about what is considered proper. Do what works; in this case perhaps browser sniffing is the best or only good option.

I never understood the problem with just using properties out of the navigator object:

<script>
    for(var item in navigator)
    {
        document.write('navigator.' + item + ': ' + navigator[item] + '<br>');
    }
</script>

They say that the navigator.userAgent is unreliable, but do your research, it can be combined with navigator.appName and navigator.vendor with high reliability I reckon.


UPDATE: March 2013

You have to test directly for the thing that you want to know, if you try to infer it, you're doing it wrong.

For example. If you want to use a feature, test for it directly, don't assume that if document.all then you can use document.uniqueID. Test for document.uniqueID directly.

Everyone knows that using navigator.userAgent to determine if window.localStorage can be used is insane, but they don't realise that ie7: document.all && window.XMLHttpRequest && !XDomainRequest && !window.opera is also doing the same thing in the opposite direction.

If you actually want to know what the user agent is, then all you can go off is the navigator object, unfortunately.

User agent string spoofing is not a problem, not yours anyway.

You can try: BrowserHawk, (http://www.cyscape.com/showbrow.asp) Which does the browser checking on the server side. This will theoritically reduce the processing on browser side when determining what to show the end users. However, I dont think this is free. Their featured customers include Yahoo, AOL, Cisco, Microsoft and Sun. So this is for production use only with large expectancies of end users visiting the site.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!