JDP 發表於 2013-6-21 15:18:34

Javascript 判斷瀏覽器和版本

2013/11/22 新增支援IE11

var Sys = {};
var ua = navigator.userAgent.toLowerCase();


//Mozilla/5.0 (Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; rv:11.0) like GeckoIE: 11 (32bit)
if (!!navigator.userAgent.match(/Trident\/7\./))
{
        //document.write(!!navigator.userAgent.match(/Trident\/7\./));
        var isIE11 = true;
}
//IE: mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0; .net clr 2.0.50727; infopath.2; .net clr 3.0.4506.2152; .net clr 3.5.30729)IE: 8.0
else if (window.ActiveXObject){
      Sys.ie = ua.match(/msie ([\d.]+)/);//正則匹配 "msie 8.0"
}
//else if (document.getBoxObjectFor){// ff升級以後就這個屬性就變成undefined
//firefox:mozilla/5.0 (windows nt 5.1; rv:17.0) gecko/20100101 firefox/17.0
else if(ua.indexOf("firefox")!=-1){
      Sys.firefox = ua.match(/firefox\/([\d.]+)/); //正則匹配"firefox/17.0"
}
//Chrome有一個MessageEvent函數,但Firefox也有。不過,好在Chrome並沒有Firefox的getBoxObjectFor函數,根據這個條件還是可以準確判斷出Chrome瀏覽器的
//chrome: mozilla/5.0 (windows nt 5.1) applewebkit/537.11 (khtml, like gecko) chrome/23.0.1271.95 safari/537.11Chrome: 23.0.1271.95
else if (window.MessageEvent && !document.getBoxObjectFor){
      Sys.chrome = ua.match(/chrome\/([\d.]+)/); //正則匹配"chrome/23.0.1271.95"
}
//Opera提供了專門的瀏覽器標誌,就是window.opera屬性
else if (window.opera){
      Sys.opera = ua.match(/opera.([\d.]+)/);
}
//Safari瀏覽器中有一個其他瀏覽器沒有的openDatabase函數,可做為判斷Safari的標誌
else if (window.openDatabase){
      Sys.safari = ua.match(/version\/([\d.]+)/);
}
//以下進行測試
if(Sys.ie) document.write('IE: '+Sys.ie);
if(isIE11) document.write('IE: 11');
if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
if(Sys.opera) document.write('Opera: '+Sys.opera);
if(Sys.safari) document.write('Safari: '+Sys.safari);Reference:
http://lyf-justdo.rhcloud.com/20 ... rowser-and-version/
http://www.monmonkey.com/javascript/liulanqi2.html

JDP 發表於 2013-11-22 13:15:58

Javascript 判斷是否為IE11

IE11 的 UserAgent 變成這樣
Mozilla/5.0 (Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; rv:11.0)

判斷方式要變成
var isIE11 = !!navigator.userAgent.match(/Trident\/7\./);

Reference: http://stackoverflow.com/questions/17447373/how-can-i-target-only-internet-explorer-11-with-javascript
頁: [1]
查看完整版本: Javascript 判斷瀏覽器和版本