网站简介:提供div+css网站标准化重构技术学习教程资料下载 其中包括:float,padding,margin,position,background等的使用方法
当前位置:Web标准化 >> 浏览器兼容 >> 正文

IE中的Javascript条件注释

2008年04月21日 来源:本站原创 浏览次数: 字号:[ ]

很多人都知道IE的HTML条件注释,但是IE的javascript可能就只有较少的人了解了,从《javascript权威指南(第五版)》摘译一篇文章,作个IE Javascript条件注释的简单介绍。

 ----------------------------------- 

Conditional Comments in Internet Explorer

Internet Explorer 中的Javascript条件注释

In practice, you'll find that many of the incompatibilities in client-side Javascript programming turn out to be IE-specific. That is, you must write code in one way for IE and in another way for all other browsers. Although you should normally avoid browser-specific extensions that are not likely to be standardized, IE supports conditional comments in both HTML and Javascript code that can be useful.

实际上,你会发现你在客户端Javascript编程的过程中的很多不兼容性都是特定于IE的。这就是说,你必须给IE和其它浏览器写不同的代码实现方式。尽管你通常避免不标准化的浏览器特定扩展,但是IE支持的HTML和Javascript的条件注释却是很有用的。

Here is what conditional comments in HTML look like. Notice the tricks played with the closing delimiter of HTML comments:

下面就是IE中的HTML条件注释的样子,注意它使用的结束分隔符的技巧。
 

<!--[if IE]>
This content is actually inside an HTML comment.
It will only be displayed in IE.
<![endif]-->
<!--[if gte IE 6]>
This content will only be displayed by IE 6 and later.
<![endif]-->
<!--[if !IE]> <-->
This is normal HTML content, but IE will not display it
because of the comment above and the comment below.
<!--> <![endif]-->
This is normal content, displayed by all browsers.

Conditional comments are also supported by IE's Javascript interpreter, and C and C++ programmers may find them similar to the  #ifdef/#endif  functionality of the C preprocessor. A Javascript conditional comment in IE begins with the text /*@cc_on and ends with the text @*/. (The cc in cc_on stands for conditional compilation.) The following conditional comment includes code that is executed only in IE:

IE的Javascript解析器也支持条件注释,C和C++程序员可能会觉得它们和C处理器的 #ifdef/#endif 功能很类似。Javascript条件注释使用字符/*@cc_on标记开始,用 @*/标记结束。
 

/*@cc_on

  @if (@_jscript)

    // This code is inside a JS comment but is executed in IE.

    alert("In IE");

  @end

  @*/

Inside a conditional comment, the keywords  @if @else , and  @end  delimit the code that is to be conditionally executed by IE's Javascript interpreter. Most of the time, you need only the simple conditional shown above:  @if (@_jscript) . Jscript is Microsoft's name for its Javascript interpreter, and the  @_jscript  variable is always true in IE. 
 

在条件注释内部,关键字 @if @else @end  区划出哪些是要被IE的Javascript解析器条件执行的。大多数时候,只需要上面所示的简单条件: @if (@_jscript) .Jscript是微软自己的Javascript解析器的名字,  @_jscript  变量在IE中的值恒为ture

With clever interleaving of conditional comments and regular Javascript comments, you can set up one block of code to run in IE and a different block to run in all other browsers:

通过条件注释和常规的Javascript注释的合理交叉结合,可以设置让IE和其它浏览器运行不同的代码。
 

/*@cc_on

  @if (@_jscript)

    // This code is inside a conditional comment, which is also a

    // regular Javascript comment. IE runs it but other browsers ignore it.

    alert('You are using Internet Explorer);

  @else*/

    // This code is no longer inside a Javascript comment, but is still 
    // inside the IE conditional comment.  This means that all browsers 
    // except IE will run this code. 
    alert('You are not using Internet Explorer');

/*@end

  @*/

Conditional comments, in both their HTML and Javascript forms, are completely nonstandard. They are sometimes a useful way to achieve compatibility with IE, however.

包括HTML形式和Javascript形式中的条件注释,都是完全非标准的,但它们有时是兼容IE的很有用的方式。

Web标准化 http://www.div-css.com

摘自:《javascript权威指南(第五版)》

?
?