我創建了一個userAgent嗅探JavaScript應用程序(用於實驗/學習的目的只有),我差不多完成了。但是,對於$ browser.version,我得到了整個userAgent字符串。我已經谷歌和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,如Gecko)Chrome/22.0.122 9.91 Safari瀏覽器/ 537.4(browser.version $)
但我想,顯然是 「Chrome瀏覽器22」 或 「鉻22.0.1229.91」
什麼是versionOffset,你從來沒有在任何地方設置它。 – aquinas
那麼,經過研究,好像我應該使用一個佔位符變量作爲基礎,所以我將其命名爲versionOffset,我應該不這樣做嗎? – ModernDesigner