javascript 检查 IE 10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16135814/
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-10-27 03:23:44  来源:igfitidea点击:

Check for IE 10

javascriptinternet-explorerinternet-explorer-10

提问by user2292674

How can I make a message box appear on page load if the user is using IE 10?

如果用户使用的是 IE 10,如何在页面加载时显示消息框?

function ieMessage() {
    alert("Hello you are using I.E.10");
}

My webpage is a JSF facelet (XHTML).

我的网页是一个 JSF facelet (XHTML)。

回答by Ian

The real way to detect this, without conditional comments and without User Agent sniffing is with conditional compilation:

在没有条件注释和用户代理嗅探的情况下,检测这一点的真正方法是使用条件编译:

<script type="text/javascript">
    var isIE10 = false;
    /*@cc_on
        if (/^10/.test(@_jscript_version)) {
            isIE10 = true;
        }
    @*/
    console.log(isIE10);
</script>

After running this code, you can use following anytime after:

运行此代码后,您可以随时使用以下内容:

if (isIE10) {
    // Using Internet Explorer 10
}

Reference: How can I detect IE10 from JS when browser mode is IE9?

参考:当浏览器模式为 IE9 时,如何从 JS 检测 IE10?



UPDATE:

更新:

To avoid minification of comments, you can use something like:

为了避免评论的缩小,您可以使用以下内容:

var IE = (function () {
    "use strict";

    var ret, isTheBrowser,
        actualVersion,
        jscriptMap, jscriptVersion;

    isTheBrowser = false;
    jscriptMap = {
        "5.5": "5.5",
        "5.6": "6",
        "5.7": "7",
        "5.8": "8",
        "9": "9",
        "10": "10"
    };
    jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();

    if (jscriptVersion !== undefined) {
        isTheBrowser = true;
        actualVersion = jscriptMap[jscriptVersion];
    }

    ret = {
        isTheBrowser: isTheBrowser,
        actualVersion: actualVersion
    };

    return ret;
}());

And access the properties like IE.isTheBrowserand IE.actualVersion(which is translated from internal values of JScript versions).

并访问像IE.isTheBrowserIE.actualVersion这样的属性(从 JScript 版本的内部值转换而来)。

回答by Alex W

In general, the practice of User Agent sniffing and conditional compilation/comments are best avoided. It is far better to use feature detection, graceful degradation, and progressive enhancementinstead. However, for the few edge cases where it is more convenient for the developer to detect the browser version, you can use the following code snippets:

一般来说,最好避免用户代理嗅探和条件编译/注释的做法。使用特征检测优雅降级渐进增强要好得多。但是,对于开发人员更方便检测浏览器版本的少数边缘情况,您可以使用以下代码片段:

This ifstatement will only execute on IE 10

if语句仅在 IE 10 上执行

// IF THE BROWSER IS INTERNET EXPLORER 10
if (navigator.appVersion.indexOf("MSIE 10") !== -1)
{
    window.alert('This is IE 10');
}

This ifstatement will only execute on IE 11

if语句仅在 IE 11 上执行

// IF THE BROWSER IS INTERNET EXPLORER 11
var UAString = navigator.userAgent;
if (UAString.indexOf("Trident") !== -1 && UAString.indexOf("rv:11") !== -1)
{
    window.alert('This is IE 11');
}

http://jsfiddle.net/Qz97n/

http://jsfiddle.net/Qz97n/

回答by Daryl Ginn

Here's a method for getting the current IE or the IE Version:

这是获取当前 IE 或 IE 版本的方法:

function IE(v) {
  return RegExp('msie' + (!isNaN(v)?('\s'+v):''), 'i').test(navigator.userAgent);
}

Here's how you can use it:

使用方法如下:

if(IE())   alert('Internet Explorer!');
if(IE(10)) alert('Internet Explorer 10!');