简体   繁体   English

条件注释 - 包含基于IE版本的javascript

[英]Conditional comments - Include javascript based on IE Version

I am running a DotNetNuke CMS website built on ASP.NET framework. 我正在运行一个基于ASP.NET框架的DotNetNuke CMS网站。

I have a few scripts in my page skin that I do not want to run on IE8 and below. 我的页面皮肤中有一些脚本,我不想在IE8及以下版本上运行。 I've been google'ing around, and I've found this IE conditional statement. 我一直在谷歌周围,我发现这个IE条件声明。

<![if gt IE 8]>

As per http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx , this snippit should include the code inbetween for any browser that is greater than IE8. 根据http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx ,对于任何大于IE8的浏览器,此snippit应包含inbetween的代码。 I attempted to use this conditional statement in the following manner: 我尝试以下列方式使用此条件语句:

<![if gt IE 8]>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4f21643b21c50811"></script>
<![endif]-->

However, this does not seem to work, and the scripts do not run on any browser. 但是,这似乎不起作用,并且脚本不能在任何浏览器上运行。 Is there a better way to accomplish this goal? 有没有更好的方法来实现这一目标? Is there a syntax error in my code? 我的代码中是否存在语法错误?

Thanks for your help! 谢谢你的帮助! Alex 亚历克斯

The conditional comment should be: 条件评论应该是:

<!--[if IE 8]>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4f21643b21c50811"></script>
<![endif]-->

with the two dashes after the exclamation point. 感叹号后面有两个破折号。

It's called a "conditional comment" because it's actually an HTML comment: 它被称为“条件评论”,因为它实际上是一个HTML评论:

<!-- this is a comment -->.

Internet Explorer (until IE9) makes an exception and parse the comments with the special format Internet Explorer(直到IE9)发出异常并使用特殊格式解析注释

<!--[ if ... ]
...
<![endif]-->

as an instruction, but for any other browsers is just a comment and it'll be ignored. 作为一个指令,但对于任何其他浏览器只是一个注释,它将被忽略。

Just wanted to add some updated information that as of IE10 and above, conditional comments are not supported, so using a conditional like so, 只是想添加一些更新的信息,从IE10及更高版本开始,不支持条件注释,因此使用条件如此,

<!--[if gt IE 8]>
<script type="text/javascript"   src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4f21643b21c50811"></script>
<![endif]-->

which includes a gt operator (per question post), means the script will actually only show up in IE9, effectively making it the same in function as writing: 其中包括一个gt运算符(每个问题帖子),意味着该脚本实际上只会出现在IE9中,有效地使其与写入功能相同:

<!--[if IE 9]>
<script type="text/javascript"   src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4f21643b21c50811"></script>
<![endif]-->

End of conditional support article: https://msdn.microsoft.com/en-us/library/ie/hh801214%28v=vs.85%29.aspx 条件支持文章结束: https//msdn.microsoft.com/en-us/library/ie/hh801214%28v=vs.85%29.aspx

Conditional comments resource: https://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx 条件评论资源: https//msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx

Just adding an information to load different scripts for IE10+ and IE9-. 只需添加一个信息来加载IE10 +和IE9-的不同脚本。

    <!--[if lt IE 10]>
         <script src="scripts/jquery-1.x.x.min.js" type="text/javascript"></script>
    <!--<![endif]-->
    <!--[if gte IE 10]><!-->
    <script src="jquery-2.x.x.min.js" type="text/javascript"></script>
    <!--<![endif]-->

Note the code at line 4, this informs IE10+ that the script tag at line 5 is not a comment 请注意第4行的代码,这通知IE10 +第5行的脚本标记不是注释

The above conditional statement will load jquery-1.xx in IE9 or lower and loads jquery-2.xx for IE10+ versions. 上面的条件语句将在IE9或更低版本中加载jquery-1.xx并为IE10 +版本加载jquery-2.xx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM