当浏览器模式是IE9时,如何从JS检测IE10? [英] How can I detect IE10 from JS when browser mode is IE9?

查看:325
本文介绍了当浏览器模式是IE9时,如何从JS检测IE10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要使用简单的javascript(没有框架)检测浏览器是IE10的浏览器模式设置。确实需要检测浏览器,它不是一个选项来检测只是功能,因为目的是减轻浏览器错误。
- 我试过常见的方式(如UA字符串或功能检测)没有成功,当我切换浏览器模式到IE9的每个方面,可以建议在IE10只是消失。


<如果你想检测你正在使用的浏览器,IE有一个特殊的功能,称为条件编译 - http://www.javascriptkit.com/javatutors/conditionalcompile.shtml



要获取版本,您可以使用 @_ jscript_version 。所以在Javascript中,我会使用:

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

我没有IE10,但是当使用IE9测试时,它似乎工作。如果我将浏览器模式更改为7或8, @_ jscript_version 具有真正的浏览器JScript版本(它保持为 9 在我的情况下)。



要查看JScript版本的列表,您可以在这里看到 - http://en.wikipedia.org/wiki/JScript#Versions 。它不列出IE10,但我认为它是10.在IE9之前,他们使用与浏览器版本不一致的数字,但是从9开始可能在正确的轨道上。你必须看看默认情况下,@_jscript_version 为10,但我认为它以10开头,并且可能有一个小版本。






UPDATE:



为避免对注释进行缩小,您可以使用类似:

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

并访问 IE.isTheBrowser IE.actualVersion (从JScript版本的内部值转换而来)。


I need to detect with just plain javascript (no frameworks) when the browser is IE10 despite the browser mode setting.

Some comments: - I do need to detect the browser, it isn't an option to detect just features since the purpose is mitigating a browser bug. - I have tried the common ways (like UA string or feature detection) with no success, when I switch browser mode to IE9 every aspect that could suggest being in IE10 just vanishes.

解决方案

If you want to detect the browser you're working with, IE has a special feature, called conditional compilation - http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

To get the version, you'd use @_jscript_version. So in Javascript, I'd use:

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

I don't have IE10, but when testing with IE9, it seems to work. If I change the browser mode to 7 or 8, @_jscript_version has the true browser JScript version (it stays as 9 in my case).

To see the list of JScript versions, you can see them here - http://en.wikipedia.org/wiki/JScript#Versions . It doesn't list IE10, but I'd assume it's 10. Before IE9, they used numbers inconsistent with the browser version, but are possibly on the right track since 9. You'll have to see what @_jscript_version is by default for 10, but I'd assume it starts with "10" and has a minor version possibly.


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.isTheBrowser and IE.actualVersion (which is translated from internal values of JScript versions).

这篇关于当浏览器模式是IE9时,如何从JS检测IE10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆