运行javascript如果检测到Internet Explorer

时间:2013-03-06 18:26:07

标签: javascript jquery qtip2

我有这段代码:

$(document).ready(function () {
    $('body a[href]').qtip({
        hide: {
            fixed: true,
            delay: 500
        },
        style: {
            classes: 'qtip-dark qtip-shadow'
        },
        position: {
            viewport: $(window)
        }
    });
    jQuery.each(jQuery.browser, function (i, val) {
        $("<div>" + i + " : <span>" + val + "</span>")
            .appendTo(document.body);
    });
});

除上述代码外,如果从此浏览器功能中检测到Internet Explorer,我将如何运行脚本? 如果检测到Internet Explorer,我想运行的脚本是:

$(document).ready(function () {
    $('body a[href]').qtip({
        hide: {
            fixed: true,
            delay: 500
        },
        style: {
            classes: 'qtip-dark qtip-shadow'
        }
    });

2 个答案:

答案 0 :(得分:6)

使用IE conditional comments

<!--[if IE]>
<script>
// run code here or link to external file
</script>
<![endif]-->

它们也是高度可定制的 - 例如,<!--[if lt IE 8]>仅适用于IE 7或更低版​​本。开发人员使用它来为IE6 / 7/8创建自定义样式表。

也就是说,您应该考虑使用功能检测而不是浏览器检测来执行任何操作。请参阅http://modernizr.com,了解可行的最佳解决方案。

答案 1 :(得分:0)

您可以使用javascript代码来检测

//Test for Internet Explorer
if (/MSIE\s([\d.]+)/.test(navigator.userAgent)) {
    //Get the IE version.  This will be 6 for IE6, 7 for IE7, etc...
    version = new Number(RegExp.$1);
}