how do I run a script only for IE 8

2019-03-22 07:57发布

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
} 

9条回答
手持菜刀,她持情操
2楼-- · 2019-03-22 08:18

Try using like this

<script type="text/javascript">
    if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
        document.write('<script src="somescript.js"><\/script>');
</script>
查看更多
走好不送
3楼-- · 2019-03-22 08:24

You can do something like this:

if (navigator.userAgent.indexOf('MSIE 8.0') !== -1) {
  // this clause will only execute on IE8
}
查看更多
混吃等死
4楼-- · 2019-03-22 08:28

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
}
查看更多
登录 后发表回答