我正在制作一种浏览器检测库(我知道用户代理嗅探并不理想)。我在quirksmode.org上找到了一个非常有用的片段,看起来像这样:
var $browser = {
init: function () {
this.name = this.searchString(this.dataName) || "An unknown browser";
this.version = this.searchVersion(navigator.userAgent)
|| this.searchVersion(navigator.appVersion)
|| "an unknown version";
},
searchString: function (data) {
for (var i=0;i<data.length;i++) {
var dataString = data[i].string;
var dataProp = data[i].prop;
this.versionSearchString = data[i].versionSearch || data[i].identity;
if (dataString) {
if (dataString.indexOf(data[i].subString) != -1)
return data[i].identity;
}
else if (dataProp) {
return data[i].identity;
}
}
},
searchVersion: function (dataString) {
var index = dataString.indexOf(this.versionSearchString);
if (index == -1) return;
return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
},
dataName: [
{
string: navigator.userAgent,
subString: "Chrome",
identity: "Chrome"
},
{ string: navigator.userAgent,
subString: "OmniWeb",
versionSearch: "OmniWeb/",
identity: "OmniWeb"
},
{
string: navigator.vendor,
subString: "Apple",
identity: "Safari",
versionSearch: "Version"
},
{
prop: window.opera,
identity: "Opera",
versionSearch: "Version"
},
{
string: navigator.vendor,
subString: "iCab",
identity: "iCab"
},
{
string: navigator.vendor,
subString: "KDE",
identity: "Konqueror"
},
{
string: navigator.userAgent,
subString: "Firefox",
identity: "Firefox"
},
{
string: navigator.vendor,
subString: "Camino",
identity: "Camino"
},
{ // for newer Netscapes (6+)
string: navigator.userAgent,
subString: "Netscape",
identity: "Netscape"
},
{
string: navigator.userAgent,
subString: "MSIE",
identity: "Internet Explorer",
versionSearch: "MSIE"
},
{ // for older Netscapes (4-)
string: navigator.userAgent,
subString: "Mozilla",
identity: "Netscape",
versionSearch: "Mozilla"
}
]
};
$browser.init();
所以现在我可以做类似
的事情alert($browser.name + $browser.version);
但我也希望能够做$ browser.author,$ browser.prefix和$ browser.engine。 谁知道我怎么能这样做?我知道我能做到
if($browser.name == "Chrome"){ var engine = "WebKit" }
但我不想只是“引擎”,我想要“$ browser.engine”
的
答案 0 :(得分:0)
jQuery Map browser
实际上被分组到引擎中,因为不推荐使用“safari”值,现在首选“webkit”值。
但是你应该使用jQuery.support。
答案 1 :(得分:0)
看一下这些链接:
请记住,您通常不应该使用此信息而不是进行功能检测(除非您必须 - 就像某些移动浏览器一样,您可能无法进行适当的功能检测因为你无论如何都是用JavaScript做的,所以不适用于此。
要使用Modernizr和yepnope进行正确的功能检测,请尽可能使用future friendly。