最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how do I run a script only for IE 8 - Stack Overflow

programmeradmin3浏览0评论

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
} 

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
} 
Share Improve this question edited Aug 5, 2013 at 21:28 Daedalus 7,7225 gold badges38 silver badges63 bronze badges asked Aug 5, 2013 at 21:25 DannyDDannyD 2,88116 gold badges57 silver badges78 bronze badges 9
  • 2 Why do you need to detect IE8 specifically? – Blender Commented Aug 5, 2013 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 Commented Aug 5, 2013 at 21:27
  • This also won't work in new versions of jQuery because they removed $.browser in version 1.9 – Ian Commented Aug 5, 2013 at 21:27
  • 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 Commented Aug 5, 2013 at 21:30
  • 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 Commented Aug 5, 2013 at 21:56
 |  Show 4 more comments

9 Answers 9

Reset to default 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
}

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]-->

Alright people, I solved it myself like this:

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

} 

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.

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>

You can do something like this:

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

$.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

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

Try using like this

<script type="text/javascript">
    if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
        document.write('<script src="somescript.js"><\/script>');
</script>
发布评论

评论列表(0)

  1. 暂无评论