如何仅在 IE 中加载脚本

How to load a script only in IE(如何仅在 IE 中加载脚本)

本文介绍了如何仅在 IE 中加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要在 Internet Explorer 浏览器中触发特定脚本!

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

我试过了:

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

不幸的是,这实际上阻止了脚本的加载.

Unfortunately this actually stops the script from being loaded.

对于问我为什么需要这个的每个人:IE 在使用某些动画时会使滚动变得非常跳跃.为了解决这个问题,我需要实现一个为 IE 提供平滑滚动的脚本.我不想将它应用到其他浏览器,因为它们不需要它,而且这个脚本虽然使滚动更平滑也让它有点不自然.

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.

推荐答案

我很好奇为什么你特别需要针对 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="somescript.js"></script>');
</script>

Regex 的前半部分 (MSIE d) 用于检测 Internet Explorer 10 及以下版本.后半部分用于检测IE11(Trident.*rv:).

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:).

如果浏览器的用户代理字符串与该模式匹配,它会将 somescript.js 附加到页面.

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

这篇关于如何仅在 IE 中加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何仅在 IE 中加载脚本

基础教程推荐