470

我通过单击具有某个类的 div 来调用如下所示的函数。

如果用户正在使用 Internet Explorer,我是否可以在启动该功能时进行检查,如果他们正在使用其他浏览器则中止/取消它,以便它只为 IE 用户运行?这里的用户都使用 IE8 或更高版本,所以我不需要介绍 IE7 和更低版本。

如果我能告诉他们正在使用哪个浏览器,那就太好了,但不是必需的。

示例函数:

$('.myClass').on('click', function(event)
{
    // my function
});
4

33 回答 33

698

几年后,Edge 浏览器现在使用 Chromium 作为其渲染引擎。
遗憾的是,检查 IE 11 仍然是一件事。

这是一个更直接的方法,因为旧版本的 IE 应该已经消失了。

if (window.document.documentMode) {
  // Do IE stuff
}

这是我的旧答案(2014):

在 Edge 中,用户代理字符串已更改。

/**
 * detect IEEdge
 * returns version of IE/Edge or false, if browser is not a Microsoft browser
 */
function detectIEEdge() {
    var ua = window.navigator.userAgent;

    var msie = ua.indexOf('MSIE ');
    if (msie > 0) {
        // IE 10 or older => return version number
        return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
    }

    var trident = ua.indexOf('Trident/');
    if (trident > 0) {
        // IE 11 => return version number
        var rv = ua.indexOf('rv:');
        return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
    }

    var edge = ua.indexOf('Edge/');
    if (edge > 0) {
       // Edge => return version number
       return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
    }

    // other browser
    return false;
}

示例用法:

alert('IEEdge ' + detectIEEdge());

IE 10 的默认字符串:

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

IE 11 的默认字符串:

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko 

Edge 12 的默认字符串:

Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0 

Edge 13 的默认字符串(谢谢@DrCord):

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 

Edge 14 的默认字符串:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/14.14300 

Edge 15 的默认字符串:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063 

Edge 16 的默认字符串:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299 

Edge 17 的默认字符串:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134 

Edge 18 的默认字符串(内幕预览):

Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17730 

在 CodePen 进行测试:

http://codepen.io/gapcode/pen/vEJNZN

于 2014-02-11T20:29:10.963 回答
526

使用下面的 JavaScript 方法:

function msieversion() 
{
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0) // If Internet Explorer, return version number
    {
        alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
    }
    else  // If another browser, return 0
    {
        alert('otherbrowser');
    }

    return false;
}

您可以在以下 Microsoft 支持网站上找到详细信息:

如何从脚本确定浏览器版本

更新:(支持 IE 11)

function msieversion() {

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer, return version number
    {
        alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
    }
    else  // If another browser, return 0
    {
        alert('otherbrowser');
    }

    return false;
}
于 2013-11-15T11:18:32.080 回答
159

如果您只想知道浏览器是否为 IE,您可以这样做:

var isIE = false;
var ua = window.navigator.userAgent;
var old_ie = ua.indexOf('MSIE ');
var new_ie = ua.indexOf('Trident/');

if ((old_ie > -1) || (new_ie > -1)) {
    isIE = true;
}

if ( isIE ) {
    //IE specific code goes here
}

更新 1:更好的方法

我现在推荐这个。它仍然非常易读,而且代码少得多:)

var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);

if ( isIE ) {
  //IE specific code goes here
}

感谢 JohnnyFun 在评论中缩短答案:)

更新 2:在 CSS 中测试 IE

首先,如果可以的话,你应该使用@supports语句而不是 JS 来检查浏览器是否支持某个 CSS 功能。

.element {
  /* styles for all browsers */
}

@supports (display: grid) {
  .element {
    /* styles for browsers that support display: grid */
  }
}

(请注意,IE 根本不支持@supports,并且会忽略放置在@supports语句中的任何样式。)

如果问题无法解决,@supports那么您可以这样做:

// JS

var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);

if ( isIE ) {
  document.documentElement.classList.add('ie')
}
/* CSS */

.element {
  /* styles that apply everywhere */
}

.ie .element {
  /* styles that only apply in IE */
}

(注意:classList对于 JS 来说相对较新,我认为,在 IE 浏览器之外,它只适用于 IE11。可能也适用于 IE10。)

如果您在项目中使用 SCSS (Sass),则可以简化为:

/* SCSS (Sass) */

.element {
  /* styles that apply everywhere */

  .ie & {
    /* styles that only apply in IE */
  }
}

更新 3:添加 Microsoft Edge(不推荐)

如果您还想将 Microsoft Edge 添加到列表中,可以执行以下操作。但是我不推荐它,因为 Edge 是一个比 IE 更有能力的浏览器。

var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident|Edge\//.test(ua);

if ( isIE ) {
  //IE & Edge specific code goes here
}
于 2014-03-21T05:59:39.353 回答
48

这将返回true任何版本的 Internet Explorer:

function isIE(userAgent) {
  userAgent = userAgent || navigator.userAgent;
  return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}

userAgent参数是可选的,默认为浏览器的用户代理。

于 2014-10-31T15:31:56.450 回答
32

您可以使用 navigator 对象来检测用户导航器,您不需要 jquery,下面的 4 条评论已经包含,所以这个片段可以按预期工作

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ){ 
 // Do stuff with Internet-Exploders ... :)
}

http://www.javascriptkit.com/javatutors/navigator.shtml

于 2013-11-15T10:55:56.180 回答
31

这就是 Angularjs 团队的做法(v 1.6.5):

var msie, // holds major version number for IE, or NaN if UA is not IE.

// Support: IE 9-11 only
/**
 * documentMode is an IE-only property
 * http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
 */
msie = window.document.documentMode;

然后有几行代码分散在各处,将其用作数字,例如

if (event === 'input' && msie <= 11) return false;

if (enabled && msie < 8) {
于 2016-05-13T00:24:24.660 回答
11

你可以简单地这样做:

var isIE = window.document.documentMode ? true : false; // this variable will hold if the current browser is IE

我知道这个问题很老,但如果有人滚动那么远,他们可以看到简单的答案:)

于 2021-06-30T15:56:36.130 回答
10

方法 01:
$.browser 在 jQuery 版本 1.3 中被弃用并在 1.9 中被删除

if ( $.browser.msie) {
  alert( "Hello! This is IE." );
}

方法02:
使用条件注释

<!--[if gte IE 8]>
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->

<!--[if lt IE 7]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->

<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>

方法03:

 /**
 * Returns the version of Internet Explorer or a -1
 * (indicating the use of another browser).
 */
function getInternetExplorerVersion()
{
    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;
}

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 );
}

方法04:
使用JavaScript/手动检测

/*
     Internet Explorer sniffer code to add class to body tag for IE version.
     Can be removed if your using something like Modernizr.
 */
 var ie = (function ()
 {

     var undef,
     v = 3,
         div = document.createElement('div'),
         all = div.getElementsByTagName('i');

     while (
     div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i>< ![endif]-->',
     all[0]);

     //append class to body for use with browser support
     if (v > 4)
     {
         $('body').addClass('ie' + v);
     }

 }());

参考链接

于 2014-07-22T08:45:31.577 回答
10

使用上面的答案;简单而简洁的返回布尔值:

var isIE = /(MSIE|Trident\/|Edge\/)/i.test(navigator.userAgent);

于 2015-08-28T19:03:59.840 回答
9

我只是想检查浏览器是否是 IE11 或更早版本,因为它们很垃圾。

function isCrappyIE() {
    var ua = window.navigator.userAgent;
    var crappyIE = false;
    var msie = ua.indexOf('MSIE ');
    if (msie > 0) {// IE 10 or older => return version number        
        crappyIE = true;
    }
    var trident = ua.indexOf('Trident/');
    if (trident > 0) {// IE 11 => return version number        
        crappyIE = true;
    }
    return crappyIE;
}   

if(!isCrappyIE()){console.table('not a crappy browser);}
于 2019-04-12T16:24:10.547 回答
8
function detectIE() {
    var ua = window.navigator.userAgent;
    var ie = ua.search(/(MSIE|Trident|Edge)/);

    return ie > -1;
}
于 2017-02-21T16:12:02.220 回答
6

使用现代化

Modernizr.addTest('ie', function () {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf('MSIE ') > 0;
    var ie11 = ua.indexOf('Trident/') > 0;
    var ie12 = ua.indexOf('Edge/') > 0;
    return msie || ie11 || ie12;
});
于 2015-06-02T20:31:35.793 回答
6

或者这个非常短的版本,如果浏览器是 Internet Explorer,则返回 true:

function isIe() {
    return window.navigator.userAgent.indexOf("MSIE ") > 0
        || !!navigator.userAgent.match(/Trident.*rv\:11\./);
}
于 2018-09-06T12:52:20.997 回答
5

还有一个简单的(但人类可读的)功能来检测浏览器是否是 IE(忽略 Edge,这一点也不差):

function isIE() {
  var ua = window.navigator.userAgent;
  var msie = ua.indexOf('MSIE '); // IE 10 or older
  var trident = ua.indexOf('Trident/'); //IE 11

  return (msie > 0 || trident > 0);
}
于 2018-03-05T01:10:50.917 回答
4

如果你不想使用用户代理,你也可以这样做来检查浏览器是否是 IE。注释代码实际上在 IE 浏览器中运行并将“false”变为“true”。

var isIE = /*@cc_on!@*/false;
if(isIE){
    //The browser is IE.
}else{
    //The browser is NOT IE.
}   
于 2014-08-15T15:37:52.000 回答
4

如果您使用的是jquery 版本 >=1.9 ,请尝试此操作,

var browser;
jQuery.uaMatch = function (ua) {
    ua = ua.toLowerCase();

    var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
        /(webkit)[ \/]([\w.]+)/.exec(ua) ||
        /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
        /(msie) ([\w.]+)/.exec(ua) || 
        ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
       /(Trident)[\/]([\w.]+)/.exec(ua) || [];

    return {
        browser: match[1] || "",
        version: match[2] || "0"
    };
};
// Don't clobber any existing jQuery.browser in case it's different
if (!jQuery.browser) {
    matched = jQuery.uaMatch(navigator.userAgent);
    browser = {};

    if (matched.browser) {
        browser[matched.browser] = true;
        browser.version = matched.version;
    }

    // Chrome is Webkit, but Webkit is also Safari.
    if (browser.chrome) {
        browser.webkit = true;
    } else if (browser.webkit) {
        browser.safari = true;
    }

    jQuery.browser = browser;
}

如果使用<1.9 的 jQuery 版本(在 jQuery 1.9 中删除了 $.browser),请改用以下代码:

$('.myClass').on('click', function (event) {
    if ($.browser.msie) {
        alert($.browser.version);
    }
});
于 2013-11-15T11:10:56.417 回答
3

我知道这是一个老问题,但如果有人再次遇到它并且在检测 IE11 时遇到问题,这里有一个适用于所有当前版本 IE 的有效解决方案。

var isIE = false;
if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
    isIE = true;   
}
于 2015-04-09T15:17:54.060 回答
3

死灵术。

为了不依赖于用户代理字符串,只需检查一些属性:

if (document.documentMode) 
{
    console.log('Hello Microsoft IE User!');
}

if (!document.documentMode && window.msWriteProfilerMark) {
    console.log('Hello Microsoft Edge User!');
}

if (document.documentMode || window.msWriteProfilerMark) 
{
    console.log('Hello Microsoft User!');
}

if (window.msWriteProfilerMark) 
{
    console.log('Hello Microsoft User in fewer characters!');
}

此外,这会检测到新的 Chredge/Edgium (Anaheim):

function isEdg()
{ 

    for (var i = 0, u="Microsoft", l =u.length; i < navigator.plugins.length; i++)
    {
        if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
            return true;
    }

    return false;
}

这会检测到铬:

function isChromium()
{ 

    for (var i = 0, u="Chromium", l =u.length; i < navigator.plugins.length; i++)
    {
        if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
            return true;
    }

    return false;
}

而这个 Safari:

if(window.safari)
{
    console.log("Safari, yeah!");
}
于 2020-06-04T12:42:06.977 回答
3

我用过这个

function notIE(){
    var ua = window.navigator.userAgent;
    if (ua.indexOf('Edge/') > 0 || 
        ua.indexOf('Trident/') > 0 || 
        ua.indexOf('MSIE ') > 0){
       return false;
    }else{
        return true;                
    }
}
于 2016-04-02T09:34:45.643 回答
2

@SpiderCode 的解决方案不适用于 IE 11。这是我以后在代码中使用的最佳解决方案,我需要浏览器检测特定功能。

IE11 不再报告为 MSIE,根据此更改列表,这是有意避免错误检测。

如果您真的想知道它是 IE,您可以做的是在 navigator.appName 返回 Netscape 时检测用户代理中的 Trident/ 字符串,例如(未经测试的);

感谢这个答案

function isIE()
{
  var rv = -1;
  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 );
  }
  else if (navigator.appName == 'Netscape')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv == -1 ? false: true;
}
于 2014-05-23T17:06:52.677 回答
2

这里有很多答案,我想添加我的输入。IE 11 在 flexbox 方面表现得如此糟糕(在此处查看它的所有问题和不一致之处,我真的需要一种简单的方法来检查用户是否正在使用任何 IE 浏览器(直到并包括 11)但不包括 Edge,因为 Edge 实际上是挺棒的。

根据此处给出的答案,我编写了一个简单的函数,返回一个全局布尔变量,然后您可以使用该变量。检查 IE 非常容易。

var isIE;
(function() {
    var ua = window.navigator.userAgent,
        msie = ua.indexOf('MSIE '),
        trident = ua.indexOf('Trident/');

    isIE = (msie > -1 || trident > -1) ? true : false;
})();

if (isIE) {
    alert("I am an Internet Explorer!");
}

这样,您只需进行一次查找,并将结果存储在变量中,而不必在每次函数调用时获取结果。(据我所知,您甚至不必等待文档准备好执行此代码,因为用户代理与 DOM 无关。)

于 2016-05-25T15:19:35.393 回答
1

下面我在谷歌搜索时发现了这样做的优雅方式---

/ detect IE
var IEversion = detectIE();

if (IEversion !== false) {
  document.getElementById('result').innerHTML = 'IE ' + IEversion;
} else {
  document.getElementById('result').innerHTML = 'NOT IE';
}

// add details to debug result
document.getElementById('details').innerHTML = window.navigator.userAgent;

/**
 * detect IE
 * returns version of IE or false, if browser is not Internet Explorer
 */
function detectIE() {
  var ua = window.navigator.userAgent;

  // Test values; Uncomment to check result …

  // IE 10
  // ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';

  // IE 11
  // ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';

  // IE 12 / Spartan
  // ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';

  // Edge (IE 12+)
  // ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';

  var msie = ua.indexOf('MSIE ');
  if (msie > 0) {
    // IE 10 or older => return version number
    return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  }

  var trident = ua.indexOf('Trident/');
  if (trident > 0) {
    // IE 11 => return version number
    var rv = ua.indexOf('rv:');
    return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  }

  var edge = ua.indexOf('Edge/');
  if (edge > 0) {
    // Edge (IE 12+) => return version number
    return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  }

  // other browser
  return false;
}
于 2016-01-21T07:09:40.393 回答
1

我在 2020 年登陆这个页面,我看到直到 IE5 所有 userAgent 字符串都有Trident,我不确定他们是否改变了任何东西。因此,仅在 userAgent 中检查 Trident 对我有用。

var isIE = navigator.userAgent.indexOf('Trident') > -1;
于 2020-10-13T04:46:27.693 回答
1

这是另一种无需查看用户代理即可检测 IE 的方法:

var usingIE="__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR" in document;
alert("You are"+(usingIE?"":"n't")+" using Internet Explorer.");

我在测试我的网站是否在 IE 上运行时偶然发现了这个,然后我转到调试器并单击文件夹图标。它有我的脚本,还有一个Dynamic Scripts我没有的文件夹。我打开它,发现很多browsertools.library.js文件。在它们里面我发现了类似的东西:

document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = undefined;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR = false;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERRORCODE = undefined;
try{
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = eval("\r\n//# sourceURL=browsertools://browsertools.library.js");
}
catch( eObj ){
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERRORCODE = eObj.number;
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_RESULT = eObj.message || eObj.description || eObj.toString();
document.__IE_DEVTOOLBAR_CONSOLE_EVAL_ERROR = true;
}

所以我用这些来测试用户的浏览器是否是IE。请注意,这仅在您想知道他们是否有 IE 时才有效,而不是他们拥有的 IE 版本。

于 2021-04-19T18:52:33.433 回答
1

更新 SpiderCode 的答案以解决字符串“MSIE”返回 -1 但与“Trident”匹配的问题。它曾经返回 NAN,但现在为那个版本的 IE 返回 11。

   function msieversion() {
       var ua = window.navigator.userAgent;
       var msie = ua.indexOf("MSIE ");
       if (msie > -1) {
           return ua.substring(msie + 5, ua.indexOf(".", msie));
       } else if (navigator.userAgent.match(/Trident.*rv\:11\./)) {
           return 11;
       } else {
           return false;
       }
    }
于 2017-05-05T19:56:15.133 回答
0

您可以检测所有 Internet Explorer(测试的最新版本 12)。

<script>
    var $userAgent = '';
    if(/MSIE/i['test'](navigator['userAgent'])==true||/rv/i['test'](navigator['userAgent'])==true||/Edge/i['test'](navigator['userAgent'])==true){
       $userAgent='ie';
    } else {
       $userAgent='other';
    }

    alert($userAgent);
</script>

见这里https://jsfiddle.net/v7npeLwe/

于 2015-07-02T13:11:26.510 回答
0

用于检测 Internet Explorer 或 Edge 版本的 JavaScript 函数

function ieVersion(uaString) {
  uaString = uaString || navigator.userAgent;
  var match = /\b(MSIE |Trident.*?rv:|Edge\/)(\d+)/.exec(uaString);
  if (match) return parseInt(match[2])
}
于 2019-04-10T04:33:40.003 回答
0

我已将此代码放在文档就绪功能中,它仅在 Internet Explorer 中触发。在 Internet Explorer 11 中测试。

var ua = window.navigator.userAgent;
ms_ie = /MSIE|Trident/.test(ua);
if ( ms_ie ) {
    //Do internet explorer exclusive behaviour here
}
于 2016-11-03T06:31:13.253 回答
0
function msieversion() {
var ua = window.navigator.userAgent;
console.log(ua);
var msie = ua.indexOf("MSIE ");

if (msie > -1 || navigator.userAgent.match(/Trident.*rv:11\./)) { 
    // If Internet Explorer, return version numbe
    // You can do what you want only in IE in here.
    var version_number=parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)));
    if (isNaN(version_number)) {
        var rv_index=ua.indexOf("rv:");
        version_number=parseInt(ua.substring(rv_index+3,ua.indexOf(".",rv_index)));
    }
    console.log(version_number);
} else {       
    //other browser   
    console.log('otherbrowser');
}
}

您应该在控制台中看到结果,请使用 chrome Inspect。

于 2016-01-07T07:00:04.963 回答
0

这仅适用于 IE 11 版本以下。

var ie_version = parseInt(window.navigator.userAgent.substring(window.navigator.userAgent.indexOf("MSIE ") + 5, window.navigator.userAgent.indexOf(".", window.navigator.userAgent.indexOf("MSIE "))));

console.log("version number",ie_version);

于 2018-03-29T09:07:32.060 回答
-1

尝试这样做

if ($.browser.msie && $.browser.version == 8) {
    //my stuff

}
于 2017-09-22T10:47:55.753 回答
-1

我想它会帮助你在这里

function checkIsIE() {
    var isIE = false;
    if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
        isIE = true;
    }
    if (isIE)  // If Internet Explorer, return version number
    {
        kendo.ui.Window.fn._keydown = function (originalFn) {
            var KEY_ESC = 27;
            return function (e) {
                if (e.which !== KEY_ESC) {
                    originalFn.call(this, e);
                }
            };
        }(kendo.ui.Window.fn._keydown);

        var windowBrowser = $("#windowBrowser").kendoWindow({
            modal: true,
            id: 'dialogBrowser',
            visible: false,
            width: "40%",
            title: "Thông báo",
            scrollable: false,
            resizable: false,
            deactivate: false,
            position: {
                top: 100,
                left: '30%'
            }
        }).data('kendoWindow');
        var html = '<br /><div style="width:100%;text-align:center"><p style="color:red;font-weight:bold">Please use the browser below to use the tool</p>';
        html += '<img src="/Scripts/IPTVClearFeePackage_Box/Images/firefox.png"/>';
        html += ' <img src="/Scripts/IPTVClearFeePackage_Box/Images/chrome.png" />';
        html += ' <img src="/Scripts/IPTVClearFeePackage_Box/Images/opera.png" />';
        html += '<hr /><form><input type="button" class="btn btn-danger" value="Đóng trình duyệt" onclick="window.close()"></form><div>';
        windowBrowser.content(html);
        windowBrowser.open();

        $("#windowBrowser").parent().find(".k-window-titlebar").remove();
    }
    else  // If another browser, return 0
    {
        return false;
    }
}
于 2017-12-18T08:06:44.230 回答
-3

您可以使用$.browser来获取名称、供应商和版本信息。

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

于 2013-11-15T10:55:52.580 回答