使用 jQuery 识别 IE9/IE10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15657614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:30:50  来源:igfitidea点击:

Identify IE9/IE10 using jQuery

jqueryhtmlinternet-explorerbrowser-detection

提问by nkt

I have a jQuery code which is working fine for Chrome/Mozilla but not IE.

我有一个 jQuery 代码,它适用于 Chrome/Mozilla,但不适用于 IE。

if ($("html").hasClass("ie")) {
    $(function(){
        $('.green-column, .red-column, .grey-column').click(function() { 
             alert ($(this).attr("data-type"));           
        });
    });
}
else {
 $(function(){
        $('.green-column, .red-column, .grey-column').click(function() { 
         $("<div title='Selected Task is:'>" + $(this).attr("data-type") + "</div>").dialog({ 
             modal: true,
             resizable: false,
             buttons: [
                {
                   text: "OK", 
                   click: function() { $( this ).dialog( "close" ); }
                }
             ]
         });
    });
});
}
</script> 
<!--[if IE 7]>    <html lang="en-us" class="ie"> <![endif]-->
<!--[if IE 8]>    <html lang="en-us" class="ie"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en-us" class="ie"> <!--<![endif]-->

So I plan to use an alert for IE9/IE10 but I am not able to differentiate between the browsers. Can anyone tell me how to identify IE9/IE10 in jQuery/HTML?

所以我计划对 IE9/IE10 使用警报,但我无法区分浏览器。谁能告诉我如何在 jQuery/HTML 中识别 IE9/IE10?

回答by dav

Can be achieved without using libraries or conditional compilation:

可以在不使用库或条件编译的情况下实现:

if (document.addEventListener) {
    alert("IE9 or greater");
}

if (window.requestAnimationFrame) {
    alert("IE10 or greater");
}

回答by Eli

Just do like this

就这样做

if ($.browser.msie  && parseInt($.browser.version, 10) > 8) {
  alert('IE9 or IE10'); 
} else {
  alert('Non IE9 or IE10');
}

This should work for jQuery version under 1.9. If you use 1.9+, read this threador consider using modernizr.

这应该适用于 1.9 以下的 jQuery 版本。如果您使用 1.9+,请阅读此线程或考虑使用modernizr

回答by Rob W

The only reliable way to detect the currently active IE version (possibly emulated) is to combine conditional compilationand a document.documentModecheck.

检测当前活动的 IE 版本(可能是模拟的)的唯一可靠方法是结合条件编译document.documentMode检查。

Conditional compilation is optional, but it allows you to never run the IE-detection script for non-IE browsers.

条件编译是可选的,但它允许您永远不要为非 IE 浏览器运行 IE 检测脚本。

For example:

例如:

if (/*@cc_on !@*/false && (
       document.documentMode === 9 || document.documentMode === 10)
   ) {
    // IE 9 or 10 (not 8 or 11!)
    document.documentElement.className += ' ie9 ie10';
}

The previous code is not safe against minifiers. If you're going to minify your code, put the conditional compilation stuff in a string, and evalit:

前面的代码对压缩器不安全。如果你要缩小你的代码,把条件编译的东西放在一个字符串中,eval它:

if (eval('/*@cc_on !@*/false') && ( ... )) { ... }

回答by dersvenhesse

Older jQuery versions before 1.9 offered $.browserto get this. Now jQuery wants you to check for functions instead via $.support.

1.9 之前的旧 jQuery 版本提供$.browser了这个功能。现在 jQuery 想让你通过$.support.

A pretty good alternative is modernizr (http://modernizr.com/). Modernizr adds classes to your root element like msieor ie9or webkitor touchand so on.

一个很好的替代方案是 Modernizr ( http://modernizr.com/)。Modernizr 将类添加到您的根元素,例如msieorie9webkitortouch等等。

So you can easily check:

因此,您可以轻松检查:

if ($('html').hasClass('ie9')) {
    // do something
} 

回答by Dany

I know this is an old question. but in case you haven't found an answer.

我知道这是一个老问题。但如果你还没有找到答案。

Show a message if the browser is not internet explorer 9 or greater

如果浏览器不是 Internet Explorer 9 或更高版本,则显示一条消息

browser={};
if (/(chrome\/[0-9]{2})/i.test(navigator.userAgent)) {
    browser.agent = navigator.userAgent.match(/(chrome\/[0-9]{2})/i)[0].split("/")[0];
    browser.version  = parseInt(navigator.userAgent.match(/(chrome\/[0-9]{2})/i)[0].split("/")[1]);
} else if (/(firefox\/[0-9]{2})/i.test(navigator.userAgent)) {
    browser.agent = navigator.userAgent.match(/(firefox\/[0-9]{2})/i)[0].split("/")[0];
    browser.version  = parseInt(navigator.userAgent.match(/(firefox\/[0-9]{2})/i)[0].split("/")[1]);
} else if (/(MSIE\ [0-9]{1})/i.test(navigator.userAgent)) {
    browser.agent = navigator.userAgent.match(/(MSIE\ [0-9]{1})/i)[0].split(" ")[0];
    browser.version  = parseInt(navigator.userAgent.match(/(MSIE\ [0-9]{1})/i)[0].split(" ")[1]);
} else if (/(Opera\/[0-9]{1})/i.test(navigator.userAgent)) {
    browser.agent = navigator.userAgent.match(/(Opera\/[0-9]{1})/i)[0].split("/")[0];
    browser.version  = parseInt(navigator.userAgent.match(/(Opera\/[0-9]{1})/i)[0].split("/")[1]);
} else if (/(Trident\/[7]{1})/i.test(navigator.userAgent)) {
    browser.agent = "MSIE";
    browser.version  = 11;
} else {
    browser.agent = false;
    browser.version  = false;
}

if (/(Windows\ NT\ [0-9]{1}\.[0-9]{1})/.test(navigator.userAgent)) {
    browser.os = "Windows";
    switch(parseFloat(navigator.userAgent.match(/(Windows\ NT\ [0-9]{1}\.[0-9]{1})/)[0].split(" ")[2])) {
    case 6.0:
        browser.osversion = "Vista";
        break;
    case 6.1:
        browser.osversion = "7";
        break;
    case 6.2:
        browser.osversion = "8";
        break;
    default:
        browser.osversion = false;
    }
} else if (/(OS\ X\ [0-9]{2}\.[0-9]{1})/.test(navigator.userAgent)) {
    browser.os = "OS X";
    browser.osversion = navigator.userAgent.match(/(OS\ X\ [0-9]{2}\.[0-9]{1})/)[0].split(" ")[2];
} else if (/(Linux)/.test(navigator.userAgent)) {
    browser.os = "Linux";
    browser.osversion = false;
}