1

I have a js file and would like to run some code only if the browser is IE 8. Is there a way I can do this?

Right now I only have this but it's for all ie browsers:

if ($.browser.msie) {
    //do stuff for IE 8
} 
Daedalus
  • 7,586
  • 5
  • 36
  • 61
DannyD
  • 2,732
  • 16
  • 51
  • 73
  • 2
    Why do you need to detect IE8 specifically? – Blender Aug 05 '13 at 21:26
  • some of my jquery mobile code does not work for IE 8 (but does for IE 9 and 10) so I want to redirect to a different page if it's IE 8. Thanks – DannyD Aug 05 '13 at 21:27
  • This also won't work in new versions of jQuery because they removed `$.browser` in version 1.9 – Ian Aug 05 '13 at 21:27
  • Post the code that is not working in IE8 it could be something simple. I would attempt to determine that issue before resorting to conditional scripts dependent upon the browser version. – Kevin Bowersox Aug 05 '13 at 21:28
  • 3
    I suggest you turn your question from "how to do something in IE8" to "how can I fix IE8 on doing this" you would be surprise by the knowledge that SO contains... – Salketer Aug 05 '13 at 21:30
  • I've already asked that question on several forums as well as here. Here is the link: http://stackoverflow.com/questions/18018967/im-trying-to-disable-my-url-from-changing-without-disabling-ajax-in-jquery-mobi – DannyD Aug 05 '13 at 21:32
  • And in case you didn't read the docs, they provide the browser version: http://api.jquery.com/jQuery.browser/ – Ian Aug 05 '13 at 21:39
  • @Ian you are deprecated :/ – Salketer Aug 05 '13 at 21:51
  • 1
    What we do that might not be best to do... We use a PHP script that determines browser version. (We use PHP because our database stores special proxy, sandboxed IE settings or some weird stuff from our customer's IT). So we have a browser version that is provided to JS (and CSS) as a class on BODY. This way, we tell jQuery to run the codes we need, then, run function X on body.IE8 so it gets executed only if needed. In functions or triggered events, we can test by either $(body.IE8).length, or $(this).closest(.IE8).length... – Salketer Aug 05 '13 at 21:56

9 Answers9

9

See Browser detection in JavaScript? and http://modernizr.com

Here is the short(est?) answer:

if (navigator.userAgent.match(/MSIE 8/) !== null) {
  // do something in IE8
}
Community
  • 1
  • 1
Hamza Kubba
  • 2,259
  • 14
  • 12
  • 3
    Can you please comment on why you downvoted this so I can learn something from you? Thanks. – Hamza Kubba Aug 05 '13 at 21:35
  • I wasn't the one who downvoted. Which is ironic because I found my answer in your link. Please see my answer. thanks – DannyD Aug 05 '13 at 21:42
  • I wonder who did, and why... I don't mind a downvote as long as I learn something from it. Anyway, I edited with a short pure javascript code snippet that should work. – Hamza Kubba Aug 05 '13 at 21:48
7

Browser specific code is almost never a good idea. However, if you must do this, you can put your code inside conditional comments.

http://www.positioniseverything.net/articles/cc-plus.html

<!--[if IE 8]>
  <script type="text/javascript">
    // this is only executed for IE 8
  </script>
<![endif]-->
seanmk
  • 1,934
  • 15
  • 28
  • how would I place this condition in a js file rather than in my html. thanks – DannyD Aug 05 '13 at 21:35
  • If you must put the conditional code in your JS, you might look at this link: http://www.quirksmode.org/js/detect.html – seanmk Aug 05 '13 at 21:38
  • @RDoolabh Just so you know, IE10 stopped supported conditional comments like this – Ian Aug 05 '13 at 21:39
  • 1
    @Ian yes, that is a good point. This will still work for IE8, but don't try to use a version number above 9, as it will be ignored. Since the way these work is that all browsers that don't understand them will treat the contents as comments, this block will still be ignored by IE10 and future browsers. – seanmk Aug 05 '13 at 21:45
  • @seanmk I guess my point was just to inform the OP that conditional comments can't universally be used, although may work for their exact question – Ian Aug 06 '13 at 05:10
2

Alright people, I solved it myself like this:

if ($.browser.msie && $.browser.version == 8) {
    //my stuff

} 
DannyD
  • 2,732
  • 16
  • 51
  • 73
1

Could you do a call in the header to redirect the page if it is less than IE9? I find myself doing this often for my clients that use a mixture of IE6-10.

In the document head I call,

<!--[if IE 7]>  <link rel="()" type="()" href="(Your link here.)"><![endif]--> 

The other option you could look into is a shim or polyfil. There are many on the internet that help bridge the gap between modern web design and older browsers. An example is Modernizr.

Nathan
  • 3,082
  • 1
  • 27
  • 42
1

You can do it with conditional comments (by https://stackoverflow.com/a/10965091/1878731):

<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->

if ($('html').is('.ie8')) {
    ...
}

Or without messing with html (by http://en.wikipedia.org/wiki/Conditional_comment):

<script>
/*@cc_on

  @if (@_jscript_version == 5.8) { // IE8 detected
    // do something
  }
@*/
</script>
Community
  • 1
  • 1
mirelon
  • 4,896
  • 6
  • 40
  • 70
1

You can do something like this:

if (navigator.userAgent.indexOf('MSIE 8.0') !== -1) {
  // this clause will only execute on IE8
}
anarchocurious
  • 590
  • 6
  • 13
1

$.browser is deprecated so you can not use this object property in new jQuery version so to support this you have to use the jQuery migrate plugin.Here is the link. https://github.com/jquery/jquery-migrate/#readme

Musa
  • 431
  • 1
  • 4
  • 3
0

You can use the document.documentMode to get the version of IE.

switch(document.documentMode)
    {
        case 8:
            //do something with IE8
            break;
        case 9:
            //do something with IE9
            break;
        case 10:
            //do something with IE10
            break;  
        case 11:
            //do something with IE11
            break;
        default:
            console.log('Other browsers...');
    }

For documentation refer here

0

Try using like this

<script type="text/javascript">
    if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
        document.write('<script src="somescript.js"><\/script>');
</script>
Deepak Kumar
  • 221
  • 3
  • 17