Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
236 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...