How to load a script only in IE

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="https://stackoverflow.com/questions/29987969/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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)