我已经创建了一个 userAgent 嗅探 javascript 应用程序(仅用于实验/学习目的),我几乎完成了。但是,对于 $browser.version,我得到了整个 userAgent 字符串。我已经 google'd 和 google'd 并找不到答案。这是我的代码:
// contemplate new $browser module
var $browser, versionOffset, ua = navigator.userAgent;
if(ua.toLowerCase().indexOf('opera') > -1) {
$browser = {
name: "Opera",
engine: "Presto",
prefix: "-o-",
publisher: "Opera Software ASA",
version: ua.substring(versionOffset + 6)
}
}
else if(ua.toLowerCase().indexOf('msie') > -1) {
$browser = {
name: "Internet Explorer",
engine: "Trident",
prefix: "-ms-",
publisher: "Microsoft",
version: ua.substring(versionOffset + 5)
}
}
else if(ua.toLowerCase().indexOf('chrome') > -1) {
$browser = {
name: "Chrome",
engine: "WebKit",
prefix: "-webkit-",
publisher: "Google Inc.",
version: ua.substring(versionOffset + 7)
}
}
else if(ua.toLowerCase().indexOf('safari') > -1) {
$browser = {
name: "Safari",
engine: "WebKit",
prefix: "-webkit-",
publisher: "Apple Computer Inc.",
version: ua.substring(versionOffset + 8)
}
}
else if(ua.toLowerCase().indexOf('firefox') > -1) {
$browser = {
name: "Firefox",
engine: "Gecko",
prefix: "-moz-",
publisher: "Mozilla",
version: ua.substring(versionOffset + 8)
}
}
所以在我的 HTML 中,我正在做:
<script>
alert($browser.name + " " + $browser.version);
</script>
但我在 chrome 中的输出是:
[JavaScript 警报]:
Chrome ($browser.name)
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.91 Safari/537.4 ($browser.version)
但我想要,显然是“Chrome 22”或“Chrome 22.0.1229.91”