46

I need a particular script to be triggered in Internet Explorer browsers Only!

I've tried this:

<!--[if IE]> 
<script></script>
<![endif]-->

Unfortunately this actually stops the script from being loaded.

EDIT: For everyone asking why I need this: IE makes scrolling extremely jumpy when using some animations. In order to address this I need to implement a script that provides smooth scrolling to IE. I don't want to apply it to other browsers as they don't need it and this script although making the scrolling smoother also makes it a bit unnatural.

David Martins
  • 1,830
  • 6
  • 22
  • 30
  • 10
    Conditional comments don't work in IE10 and above, assuming you're running IE10/IE11 it won't be triggered. – dsgriffin May 01 '15 at 13:44
  • This may helpful :http://stackoverflow.com/questions/5505155/how-not-to-load-a-script-in-ie – Antima Gupta May 01 '15 at 13:46
  • Why are you trying to load a particular script in IE only ? – Alex May 01 '15 at 13:47
  • I thing the check for ActiveXObject will be usefull: http://stackoverflow.com/questions/19638981/window-activexobject-difference-in-ie11 – Georgi Naumov May 01 '15 at 14:02
  • possible duplicate of [jQuery: check if user is using IE](http://stackoverflow.com/questions/19999388/jquery-check-if-user-is-using-ie) – dsgriffin May 01 '15 at 14:07
  • The ActiveXObject property is no longer supported; see https://msdn.microsoft.com/library/dn423948(v=vs.85).aspx for details. To the OP, why are you trying to load a script only for IE? IE has greatly improve cross-browser compatibility and many IE-only scripts attempt to use features that are out-of-date or no longer supported. Feature detection is preferred over user-agent detection. Perhaps there's an alternate solution to the problem you're trying to solve? – Lance Leonard May 01 '15 at 15:18

6 Answers6

91

I'm curious why you specifically need to target IE browsers, but the following code should work if that really is what you need to do:

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

The first half of the Regex (MSIE \d) is for detecting Internet Explorer 10 and below. The second half is for detecting IE11 (Trident.*rv:).

If the browser's user agent string matches that pattern, it will append somescript.js to the page.

nderscore
  • 4,182
  • 22
  • 29
  • 5
    Well, it happens that IE has this fabulous feature called smooth scrolling ("Tools>Internet Options>Advanced>"Use Smooth Scrolling") that comes selected by default and actually seams to create all sort of problems instead of solving any. In my particular case I have a number of sliding-in animations that only on IE make the scrolling extremely jumpy unless you have that smooth scrolling turned off. So I need to add a scrolling script for IE only. – David Martins May 01 '15 at 14:12
  • Hello, I've some requirements in which I need some JS file to call in all browser but not in IE10 and IE9. which condition will work for this? – Yashpal Sindhav Aug 29 '17 at 17:39
  • this looks nice, but what about if you want immediate execution? – codepleb May 14 '20 at 14:42
  • 1
    @codepleb it should run synchronously afterwards because of `document.write`, but I'm not sure if I'd recommend this old answer today anyway, given how there are libraries out there that are better tested and solve this problem more safely. – nderscore May 22 '20 at 04:35
  • six years later, the main reason to need this is because IE11 just won't effing die, and everything else has long since moved on to ES6 whereas IE11 is stuck in the now-dead ES5 era. Making IE11 pull special "for ie11 only" unoptimized ES5 versions of what every other browser is getting a modern, optimized ES6 js payload for has actually become more important, rather than less important =( – Mike 'Pomax' Kamermans Feb 18 '21 at 21:51
19

currentScript is supported in all browsers besides IE

<script>
    // self-invoked wrapper for scoping the `document` variable
    !function( d ) {
        if( !d.currentScript ){
            var s = d.createElement('script')
            s.src = 'ie.js'
            d.head.appendChild(s)
        }
    }(document)
</script>

The above will conditionally append a script file only for IE browsers.

vsync
  • 118,978
  • 58
  • 307
  • 400
  • 3
    This is by far the most elegant answer. – nardecky Oct 28 '19 at 21:32
  • 1
    I like this tactic much better than relying on user-agent parsing. `currentScript` detection will fail on Opera Mini, too, however. I'd suggest checking for `!(window.CSS.supports || window.supportsCSS)` if you want to ensure _only_ IE gets the extra script. ([support table](https://caniuse.com/#feat=css-supports-api)) – Andron Feb 20 '20 at 21:40
  • Why do we need a `!` before `function(d)`? – Bruce Sun May 03 '20 at 08:17
  • 1
    @BruceSun - it's shorter version for self-invoked functions - [read more here](https://stackoverflow.com/q/3755606/104380) – vsync May 03 '20 at 11:27
  • I wish IE to never support `currentScript` :D – Jamille May 27 '22 at 11:23
5

If someone still looking at running IE specific Javascript then this code still works tested in IE(11), and non IE browsers(Chrome,Firefox,Edge)

<script type="text/javascript">
    if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
        document.write('<script src="../nolng/js/ex1IE.js"><\/script>
        <script src="../nolng/js/ex2IE.js"><\/script>');
    else
        document.write('<script src="../nolng/js/ex1.js"><\/script>
       <script src="../nolng/js/ex2.js"><\/script>');
</script>
Eonasdan
  • 7,563
  • 8
  • 55
  • 82
Vikram
  • 69
  • 1
  • 5
4

You could modify this script to run your IE specific JavaScript:

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
  alert('IE ' + parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
else alert('otherbrowser');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Alex
  • 21,273
  • 10
  • 61
  • 73
  • Looking at the code, it doesn't require JQuery, but I can't test this at the moment. – Alex Oct 29 '19 at 12:01
1

You can use javascript to detect IE and insert your script dynamicaly:

var ua = window.navigator.userAgent;
if (ua.indexOf("MSIE ") != -1|| !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.setAttribute('src', 'YOUR_JS_SCRIPT.js');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('async', 'false');
    head.appendChild(script);
}

if you can use jQuery you can use shorter code:

if (ua.indexOf("MSIE ") != -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
    $.getScript('YOUR_JS_SCRIPT.js');
}

Solution found in this post Check if user is using IE with jQuery

Community
  • 1
  • 1
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • IE11 does not have MSIE in the user agent string. – nderscore May 01 '15 at 13:54
  • You should also check that the result of `indexOf` is greater than `-1`. If the user agent doesn't contain `MSIE` it will return `-1` which is a truthy value in an if statement. – nderscore May 01 '15 at 14:15
-1

Feature detection is a better approach - I recommend looking into Modernizr to progressively enhance your solution when devices/browsers are capable of supporting it.

This CSS based approach sometimes works too, depending on what you need it for.

Put this in the <head>

<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>    <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>    <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class=""> <!--<![endif]-->

reference article - http://www.paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/

Brian Muenzenmeyer
  • 1,026
  • 1
  • 7
  • 17
  • 2
    As some comments mention - this is increasing not useful due to newer browsers not supporting it – Brian Muenzenmeyer May 01 '15 at 13:48
  • 1
    modernizer is nice but only if a given feature is in the list, also why should one use a multi-KB script to detect something where you specifically know only IE needs it which can be detected with FAR less code (just a few bytes if conditional comments worked – My1 Jul 22 '19 at 19:04