【问题标题】:Running a javascript IF internet explorer is detected如果检测到 Internet Explorer,则运行 javascript
【发布时间】:2013-02-21 16:52:10
【问题描述】:

我有这个代码:

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

【问题讨论】:

  • 请注意,浏览器检查不是最理想的。API 也指出了这一点: 描述:包含用户代理的标志,从 navigator.userAgent 读取。我们建议不要使用此属性;请尝试改用特征检测(参见 jQuery.support)。 jQuery.browser 可能会在 jQuery 的未来版本中移动到插件中。
  • @Sirko Read the docs -- jQuery.browser 已被弃用,并在 1.9 中被完全删除。
  • 我没有看到那个重复项,对于像我这样对 jquery 非常陌生的人来说,等等。这似乎对我的具体问题没有帮助。如果我也复制了 StackOverflow 的新内容,我深表歉意。我应该删除这篇文章吗?

标签: javascript jquery qtip2


【解决方案1】:

使用IE conditional comments

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

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

也就是说,您确实应该考虑使用 特征检测 而不是 浏览器检测 来实现您想要实现的任何功能。请参阅http://modernizr.com,了解可以说是迄今为止设计的最佳解决方案。

【讨论】:

  • 感谢您的帮助和链接!我将切换到特征检测。
【解决方案2】:

您可以使用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);
}

【讨论】:

  • jQuery.browser 自 jQuery v1.3 起已弃用。并已在 v1.9 中完全删除。
猜你喜欢
  • 2018-04-01
  • 2019-11-20
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
相关资源
最近更新 更多