【问题标题】:How do I detect IE 8 with jQuery now that $.browser is deprecated? [duplicate]现在 $.browser 已被弃用,如何使用 jQuery 检测 IE 8? [复制]
【发布时间】:2012-07-07 08:49:09
【问题描述】:

可能重复:
Best way to detect that HTML5 <canvas> is not supported

既然 $.browser is being taken out of jQuery 支持 $.support,我将如何使用 jQuery 或纯 javascript 检测 IE8?

if ($.browser.msie  && parseInt($.browser.version, 10) === 8) {
  alert('IE8'); 
}

抱歉,HTML 不适用于我。所以我不能这样做:

<!--[if IE 8]>
<script type="text/javascript">
    ie = 8;
</script>
<![endif]-->

具体来说,我希望使用 canvas 标签,而 IE8 不支持 canvas,但是 jQuery.support 没有检测到 canvas 支持。

【问题讨论】:

  • 这样做的全部原因是您不应该需要测试IE8。你为什么要这么做?
  • 该链接并没有说它已被弃用,它只是说明特征检测比浏览器检测更推荐。
  • 你需要在 IE 中做什么,这是不可能的?使用 jquery 的全部意义在于忘记您正在使用哪个浏览器
  • 有时人们会问错问题。这个问题可能是:“如何使用正则表达式在字符串中查找大于某个数字的数字?”为什么?因为我需要查看浏览器代理字符串。 “为什么?”因为我需要检测浏览器是否大于IE8。为什么? “因为我想看看是否支持canvas。”啊哈!所以你真正想做的是看看是否支持画布。就您的原始问题寻求帮助很重要。不要过多地选择 OP :)

标签: javascript jquery internet-explorer internet-explorer-8


【解决方案1】:

改为测试该功能:

var el = document.createElement('canvas');
if (el.getContext && el.getContext('2d')) {
    // canvas supported 
}

编辑:

就像 Brandon Boone 的 link suggested in comments 一样,这里有一个函数:

function isCanvasSupported(){
  var elem = document.createElement('canvas');
  return !!(elem.getContext && elem.getContext('2d'));
}
【解决方案2】:

如果 jQuery 不能满足您的需求,我建议改用 Modernizr 进行特征检测。

当然,在$.browser 被弃用之前,另一种选择就是坚持使用旧版本的jQuery。它仍然像往常一样工作。没有人强迫您升级到最新版本。

最后,您声明您无法编辑 HTML,因此您无法使用条件 cmets。但是,值得指出的是,IE 还支持与条件 cmets 等效的 Javascript。我假设您将能够使用此功能。

以下代码取自Wikipedia - 根据需要进行修改:

<script>
/*@cc_on

  @if (@_jscript_version == 10)
    document.write("You are using IE10");

  @elif (@_jscript_version == 9)
    document.write("You are using IE9");

  @elif (@_jscript_version == 5.8)
    document.write("You are using IE8");

  @elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
    document.write("You are using IE7");

  @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
    document.write("You are using IE6");

  @elif (@_jscript_version == 5.5)
    document.write("You are using IE5.5");

  @else
    document.write("You are using IE5 or older");

  @end

@*/
</script>

【讨论】:

  • Modernizer 很棒,我通常使用它,但它没有安装在这个项目上。感谢您的评论。
  • @BishopZ - 我已经为答案添加了其他替代解决方案。
  • 哇,很有趣,谢谢!
猜你喜欢
  • 2011-01-13
  • 2012-03-27
  • 2013-01-27
  • 2020-10-10
  • 2021-10-24
  • 2019-07-07
  • 2018-12-06
  • 2011-12-16
  • 2020-12-03
相关资源
最近更新 更多