How to load a script only in IE

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.


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.


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>