2013-05-07 75 views
0

我使用下面的函數來檢測我的IE版本:如何檢測IE版本

function isIE() 
{ 
    var myNav = navigator.userAgent.toLowerCase(); 
    return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false; 
} 

此代碼將返回IE版本,如果使用IE瀏覽器,如果其他瀏覽是將返回false用過的。它可以在IE6,IE9和Chrome中使用。當我嘗試Firefox最新版本v20時,它不起作用(網站掛起)。你們知道爲什麼嗎?

或者是否有任何其他功能可以用來檢測IE版本?

+0

「不起作用」表示它返回true? – 2013-05-07 06:49:22

+0

它不會返回任何東西。它掛在那裏... – Coolguy 2013-05-07 06:50:01

+0

你沒有在HTML中檢測到,這是Javascript ... – dtech 2013-05-07 06:50:51

回答

1

jQuery有$ .browser:http://api.jquery.com/jQuery.browser/

你可以這樣做:

if ($.browser.msie){ 
} 
+1

不幸的是,這已被棄用了很長一段時間,並已在v1.9和j2的v2中被刪除。 – 2013-05-07 07:06:24

+0

@VincentMcNabb true。在1.9我正在使用jQuery的遷移插件:) – basarat 2013-05-07 07:08:05

0

我用這個

public static class DeviceHelper 
{ 

    public static bool IsMobile(string userAgent) 
    { 
     userAgent = userAgent.ToLower(); 
     var currentSubContent = NodeLocator.GetNodeOfExactType<Language>(); 

     var isMobileDevicesActivatedInBackend = currentSubContent.ActiveForMobileDevices; 
     if (isMobileDevicesActivatedInBackend) 
     { 
      return userAgent.Contains("iphone") | 
      userAgent.Contains("ppc") | 
      userAgent.Contains("windows ce") | 
      userAgent.Contains("blackberry") | 
      userAgent.Contains("opera mini") | 
       //userAgent.Contains("mobile") | 
      userAgent.Contains("palm") | 
      userAgent.Contains("portable") | 
      (userAgent.Contains("android") && userAgent.Contains("mobile")) | 
      (userAgent.Contains("windows") && userAgent.Contains("mobile")); 

     } 
     else 
     { 
      return false; 
     } 
    } 
} 

然後,你可以添加你從這裏所需要的:檢測版本MSIE

HERE

或jQuery的只是使用他們的API:

if ($.browser.msie){ 
} 
+0

但我沒有使用移動應用程序...我用筆記本電腦。 – Coolguy 2013-05-07 06:59:26

+0

@Coolguy只是一個可以讓你自己使用的功能嗎? – 2013-05-07 07:03:45

0

使用此功能,它的工作原理。

function checkVersion() { 
     var msg = "You're not using Internet Explorer."; 
     var ver = getInternetExplorerVersion(); 

     if (ver > -1) { 
      if (ver >= 8.0) 
       msg = "You're using a recent copy of Internet Explorer." 
      else 
       msg = "You should upgrade your copy of Internet Explorer."; 
     } 
     alert(msg+" version ->"+ver); 
    } 
0

這是非常醜陋,但直截了當,使用IE條件註釋。

編輯:這裏是一個基於wikipedia's example

<script> 
    var internet_explorer_version = -1; 

/*@cc_on 

    @if (@_jscript_version == 10) 
    internet_explorer_version = 10; 

    @elif (@_jscript_version == 9) 
    internet_explorer_version = 9; 

    @elif (@_jscript_version == 5.8) 
    internet_explorer_version = 8; 

    @elif (@_jscript_version == 5.7 && window.XMLHttpRequest) 
    internet_explorer_version = 7; 

    @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest)) 
    internet_explorer_version = 6; 

    @elif (@_jscript_version == 5.5) 
    internet_explorer_version = 5.5; 

    @end 

@*/ 
</script> 

條件註釋一個更好的版本只能通過IE4 +支持。較早的IE或非IE瀏覽器將跳過所有檢查,並留下-1。這個腳本檢查5.5+,這應該是足夠遠的任何你需要的東西。

0
/** 
* Detects browser endgine and browser version. 
* @class BrowserDetect 
* @property {string} browser browser engine 
* @property {number} version browser version 
* @return {object} 
*/ 
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" 
     } 
    ] 
}; 
0

你可以使用正則表達式。

<script type="text/javascript"> 
var userAgent = navigator.userAgent; 

var internet_version = -1; 
userAgent.replace(/MSIE .{1,5};/g, function(findS) { 
    internet_version = parseFloat(findS.substring(5, findS.length-1)); 
}); 

alert(internet_version); 
</script> 
0

這個工作對我來說,我已經通過IE11測試了IE9。 IE11中的useragent字符串與之前的版本不同,這就是爲什麼這裏有兩個不同的測試。

if (/msie|trident/ig.test(navigator.userAgent)) { 
    var matches = navigator.userAgent.match(/MSIE\s([\d\.]+)/) || navigator.userAgent.match(/rv:([\d\.]+)/); 
    var ieVersion = parseInt(matches[1], 10); 
} 
0

從MSDN得到這個腳本:

function getInternetExplorerVersion() 
    // Returns the version of Internet Explorer or a -1 
    // (indicating the use of another browser). 
    // http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx 
    { 
     var rv = -1; // Return value assumes failure. 
     if (navigator.appName == 'Microsoft Internet Explorer') 
     { 
     var ua = navigator.userAgent; 
     var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); 
     if (re.exec(ua) != null) 
      rv = parseFloat(RegExp.$1); 
     } 
     return rv; 
    } 

就個人而言,我永遠只是應用類<html>,使用條件註釋。稍後,我在javascript中檢查類名。