0

How to not run a javascript function in IE.I'm having a number of functions in my javascript file..While i need to stop some script from running in Internet explorer. I know the below code is used to run this script file if not IE..

<!--[if !IE]><!--><script src="script.js"></script><!--<![endif]-->

How can i skip some function in js..like below I tried to skip the method b in IE..but it dont work..

function a{alert(a);}
<!--[if !IE]>
function b{alert(b);}
<![endif]-->
function c{alert(c);}
function d{alert(d);}

but the method b is always called..How can i prevent it from running in IE browsers..

Lucky
  • 16,787
  • 19
  • 117
  • 151
  • Put it in it's own script tag? – MDEV Sep 16 '13 at 10:21
  • 1
    possible duplicate of [Detect IE version in Javascript](http://stackoverflow.com/questions/10964966/detect-ie-version-in-javascript) – Jorge Y. C. Rodriguez Sep 16 '13 at 10:22
  • Man, you're in muddy waters ... – Dimitar Dimitrov Sep 16 '13 at 10:22
  • 2
    Why modernizer @ShashiBhushan, that it is adding another extra library for something it can work better in pure js ... – Jorge Y. C. Rodriguez Sep 16 '13 at 10:23
  • 1
    Presumably you want to not run something in IE because it's missing features, or something? The point of using Modernizr is that instead of targeting a particular browser, you base it on available features - so if there are any other *smaller* browsers that are also missingthose features, then your site doesn't break by making incorrect assumptions. – cloudfeet Sep 16 '13 at 10:32

2 Answers2

3

IE10 doesn't support conditional comments.

If you don't want to run a function in some browsers, then suppress it with feature (AKA object) detection, not user agent detection.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Actually what i do it is a bit nasty, but if there is some functions i don't want to run in IE I add this ::

    <?php if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == false)):?>

       <script src="js/noIEfunctions.js"></script>

    <?php endif;?>
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
  • 1
    FWIW, this code will not work in IE11. User-Agent sniffing is evil and should be avoided. – EricLaw Sep 16 '13 at 18:56
  • The IE team took out the MSIE token *explicitly* to break code that was doing foolish User-Agent detection instead of proper capabilities detection. Many webpages work just fine in IE10 or IE11 but due to outdated UA-string checks the pages weren't working. Hence the change. – EricLaw Sep 16 '13 at 19:29