1

Possible Duplicate:
Best way to detect that HTML5 <canvas> is not supported

Now that $.browser is being taken out of jQuery in favor of $.support, how would I detect IE8 with jQuery, or with plain javascript?

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

Sorry, but HTML will not work for me. So I can not do this:

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

Specifically, I am looking to do work with the canvas tag and IE8 does not support canvas, but jQuery.support does not detect for canvas support.

Community
  • 1
  • 1
BishopZ
  • 6,269
  • 8
  • 45
  • 58
  • 2
    The whole reason for doing this is that you shouldn't *need* to test for IE8. Why are you trying to? – lonesomeday Jul 06 '12 at 21:33
  • 1
    That link doesn't say its being deprecated, it is just explaining that feature detection is more recommended than browser detection. – Kyeotic Jul 06 '12 at 21:33
  • @Tyrsius [It's deprecated](http://api.jquery.com/jQuery.browser/) – lonesomeday Jul 06 '12 at 21:34
  • @Tyrsius "specific properties may be removed when they are no longer needed internally to improve page startup performance" But still, feature detection is better. – TheZ Jul 06 '12 at 21:34
  • 2
    what do you need to do in IE that is so impossible? the whole point of using jquery is to forget which browser you are working on – Ibu Jul 06 '12 at 21:34
  • In what feature, specifically, do you need special handling for IE8? – Ortiga Jul 06 '12 at 21:34
  • @Everyone, oh. I stand corrected. – Kyeotic Jul 06 '12 at 21:35
  • 3
    @BishopZ http://stackoverflow.com/questions/2745432/best-way-to-detect-that-html5-canvas-is-not-supported – lonesomeday Jul 06 '12 at 21:40
  • 1
    Sometimes people ask the wrong question. This question could have been: "How do I look for a number in a string that is greater than a certain number using a regex?" Why? Because I need to look at a browser agent string. "Why?" Because I need to detect if the browser is greater than IE8. Why? "Because I want to see if canvas is supported." Ah ha! So what you REALLY want to do is see if canvas is supported. Asking for help with your ORIGINAL problem is important. Not to pick on the OP too much :) – aquinas Jul 06 '12 at 21:48
  • will help you: [http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx](http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx) – raymarch Jul 06 '12 at 21:41

2 Answers2

8

Test for the feature instead:

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

Edit:

As in the link suggested in comments by Brandon Boone, here's a function that does it:

function isCanvasSupported(){
  var elem = document.createElement('canvas');
  return !!(elem.getContext && elem.getContext('2d'));
}
Community
  • 1
  • 1
Ortiga
  • 8,455
  • 5
  • 42
  • 71
2

If jQuery doesn't do what you need, I suggest using Modernizr for feature detection instead.

The other option, of course, is simply to stick with the older version of jQuery, before $.browser was deprecated. It still works just as well as it always did; no-one is forcing you to upgrade to the latest version.

Finally, you state that you can't edit the HTML so you can't use conditional comments. However, it's worth pointing out that IE also supports a Javascript equivalent to conditional comments. I would assume that you would be able to use this feature.

The following code is taken from Wikipedia - adapt it as necessary:

<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>
Spudley
  • 166,037
  • 39
  • 233
  • 307