首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后条件评论时代的浏览器检测?

后条件评论时代的浏览器检测?
EN

Stack Overflow用户
提问于 2014-06-25 13:19:05
回答 2查看 173关注 0票数 1

Internet Explorer 10+不再支持条件注释。好的,太棒了。

这是我需要的:

代码语言:javascript
复制
    (if this browser is Internet Explorer of ANY version) {
         //apply IE CSS class here
    } else {
         //apply non IE CSS class here
    }

我已经尝试了所有我能在互联网上找到的东西,没有一个不影响所有浏览器的。例如,以下代码不起作用,因为"Browser is nott IE is printed on all Browser:

代码语言:javascript
复制
    <!DOCTYPE html>
    <!--[if lte IE 8]><html class="ie8 no-js" lang="en">IE 8-10<![endif]-->
    <!--[if lte IE 10]><html class="ie10 no-js" lang="en">ID 8-<![endif]-->
    <script>
    var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
    if (!isIE) {
    document.write("<html class='not-ie no-js' lang='en'>");
    document.write("Browser is nott IE.");
    }
    </script>
EN

回答 2

Stack Overflow用户

发布于 2014-06-27 02:36:55

代码语言:javascript
复制
navigator.sayswho= (function(){
    var ua= navigator.userAgent, tem, 
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\bOPR\/(\d+)/)
        if(tem!= null) return 'Opera '+tem[1];
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return M.join(' ');
})();

代码将返回浏览器的名称。您可以根据返回值执行条件语句。

感谢u/kennebec的脚本。效果很好。

票数 1
EN

Stack Overflow用户

发布于 2014-06-27 04:12:40

虽然功能检测通常是区分支持和非支持浏览器版本的最经济的方法,但检测IE --及其版本--实际上是小菜一碟:

代码语言:javascript
复制
var uA = navigator.userAgent;
var browser = null;
var ieVersion = null;
var htmlTag = document.documentElement;

if (uA.indexOf('MSIE 6') >= 0) {
    browser = 'IE';
    ieVersion = 6;
}
if (uA.indexOf('MSIE 7') >= 0) {
    browser = 'IE';
    ieVersion = 7;
}
if (document.documentMode) { // as of IE8; strictly IE proprietary 
    browser = 'IE';
    ieVersion = document.documentMode;
}

// Using it can be done like this: 
if (browser == 'IE' && ieVersion == 11)
    htmlTag.className += ' ie11';

一个是在较低的模式/视图中捕获较高的is,以及使用此脚本,包括兼容性。我包含了版本部分,因为任何认真的web开发人员迟早都会遇到IE版本的差异。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24400701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档