2013-09-23 282 views
-3

我想顯示消息「您沒有使用Chrome瀏覽器,點擊此處下載Chrome瀏覽器,但如果瀏覽器使用的是Safari瀏覽器,我不希望顯示消息。 (使用IE瀏覽器(太糟糕了)和FF(太重)或任何其他瀏覽器(除Safari之外,因爲不錯,並且因爲任何其他第三方瀏覽器可能安裝在Apple OS上都會有不好的表現)使用Chrome(理想選擇對於使用Javascript檢測Chrome瀏覽器和Safari瀏覽器

我的應用程序的最佳用戶體驗),我發現了一個腳本,誰似乎很好地適應於做一個可靠的檢測,但我不知道如何自定義代碼:

var BrowserDetect = { 
init: function() { 
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; 
    this.version = this.searchVersion(navigator.userAgent) 
     || this.searchVersion(navigator.appVersion) 
     || "an unknown version"; 
    this.OS = this.searchString(this.dataOS) || "an unknown OS"; 
}, 
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)); 
}, 
dataBrowser: [ 
    { 
     string: navigator.userAgent, 
     subString: "Chrome", 
     identity: "Chrome" 
    }, 
    { 
     string: navigator.vendor, 
     subString: "Apple", 
     identity: "Safari", 
     versionSearch: "Version" 
    } 
] 

}; 
BrowserDetect.init(); 

有人可以幫忙嗎?如果需要我在這裏找到該腳本:

http://www.quirksmode.org/js/detect.html

+7

爲什麼你不正確地寫你的web應用程序,而不是強迫用戶使用特定的瀏覽器? – Blender

+0

爲什麼你不嘗試類似這樣的東西:http://stackoverflow.com/a/12625944/487940。然後,您可以稍作修改以顯示您的消息。此外,強迫用戶瀏覽器的主意不好。如果需要,您應該展示一些更友好的信息:「爲獲得最佳體驗,請下載並使用Google Chrome」。 IE已經有了很大的改進,所以不要低估它。 – harsimranb

+6

檢測功能,而不是瀏覽器 – Eric

回答

2

回答這個問題... :) 如果你真的想使用它,那麼就使用BrowserDetect.browser作爲指南。 我使用了http://www.quirksmode.org/js/detect.html的代碼。

<html> 
<head> 
<script type="text/javascript"> 
<!-- 
var BrowserDetect = { 
init: function() { 
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; 
    this.version = this.searchVersion(navigator.userAgent) 
     || this.searchVersion(navigator.appVersion) 
     || "an unknown version"; 
    this.OS = this.searchString(this.dataOS) || "an unknown OS"; 
}, 
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)); 
}, 
dataBrowser: [ 
    { 
     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: "Explorer", 
     versionSearch: "MSIE" 
    }, 
    { 
     string: navigator.userAgent, 
     subString: "Gecko", 
     identity: "Mozilla", 
     versionSearch: "rv" 
    }, 
    {  // for older Netscapes (4-) 
     string: navigator.userAgent, 
     subString: "Mozilla", 
     identity: "Netscape", 
     versionSearch: "Mozilla" 
    } 
], 
dataOS : [ 
    { 
     string: navigator.platform, 
     subString: "Win", 
     identity: "Windows" 
    }, 
    { 
     string: navigator.platform, 
     subString: "Mac", 
     identity: "Mac" 
    }, 
    { 
      string: navigator.userAgent, 
      subString: "iPhone", 
      identity: "iPhone/iPod" 
    }, 
    { 
     string: navigator.platform, 
     subString: "Linux", 
     identity: "Linux" 
    } 
] 

}; 
BrowserDetect.init(); 


</script> 
//--> 
</head> 
<body> 
<script type="text/javascript"> 
<!-- 
document.write('<p class="accent">You\'re using ' + BrowserDetect.browser + ' ' + BrowserDetect.version + ' on ' + BrowserDetect.OS + '!</p>'); 
// --> 
</script> 
<script type="text/javascript"> 
if((BrowserDetect.browser == 'Chrome') || (BrowserDetect.browser == 'Safari')) { 
alert('Good job!'); 
} else { 
alert('You are living in a cave, dude! Change your browser.'); 
    if(BrowserDetect.browser == 'firefox') { 
alert('I mean - firefox is OK, but bit heavy'); 
    } 
} 
</script> 

</body> 
</html>