如何仅在IE中加载脚本 [英] How to load a script only in IE

查看:115
本文介绍了如何仅在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>

正则表达式的前半部分( 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中加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆