`

js条件编译字符@cc_on,@if,@elif ,@else ,@end

阅读更多

Conditional Compilation of JScript/ JavaScript in IE

In IE, there is a little known feature called conditional compilation. Supported since IE4, this feature starting getting some attention when it began showing up in some Ajax related JavaScripts. An absolute form of object detection, conditional compilation lets you dictate to IE whether to compile certain parts of your JScript or JavaScript code depending on predefined and user defined conditions. Think of it as conditional comments for your script that can also be molded to work gracefully with non IE browsers as well.

Syntax Overview

Conditional compilation is activated by using the @cc_on statement inside your script, or by directly using an @if or @set statement that are part of the logic of CC. Here's an illustrative example:

<script type="text/javascript">

/*@cc_on
document.write("JScript version: " + @_jscript_version + ".<br>");
   /*@if (@_jscript_version >= 5)
      document.write("JScript Version 5.0 or better.<br \/>");
      document.write("This text is only seen by browsers that support JScript 5+<br>");
   @else @*/
      document.write("This text is seen by all other browsers (ie: Firefox, IE 4.x etc)<br>");
   /*@end
@*/

</script>

Example:

<!----> <script type="text/javascript"></script> JScript version: 5.6.
JScript Version 5.0 or better.
This text is only seen by browsers that support JScript 5+
<!---->

If you're using IE (of any version), you should see the first document.write() rendered, and for IE5+, the following two document.write() as well (since JScript 5 is supported by IE5+). The last document.write() method is served only to non IE5+ browsers, whether it's Firefox, Opera, IE4, you name it. Conditional compilation relies on tag teaming with the comment tag, similar to in Conditional Comments, to ensure it works harmoniously in all browsers.

When working with Conditional Compilation, it's best to first activate it via the @cc_on statement, as only then can you also include comment tags in your script in a way that ensures browser compatibility, as shown in the example above.

@if, @elif, @else, and @end statements

So with the formalities out of the way, here are the conditional statements at your disposal for conditional compilation:

  •  @if
  • @elif
  • @else
  • @end

Lets see some "eccentric" examples now.

if else logic (IE exclusive)

/*@cc_on
   @if (@_win32)
      document.write("OS is 32-bit. Browser is IE.");
   @else
      document.write("OS is NOT 32-bit. Browser is IE.");
   @end
@*/

Here the entire script is only rendered by IE browsers and ignored by all else, and depending on the bit of your OS, a different message is shown. Contrast that with the next example...

if else logic II (other browsers inclusive)

/*@cc_on
   /*@if (@_win32)
      document.write("OS is 32-bit, browser is IE.");
   @else @*/
      document.write("Browser is not IE (ie: is Firefox) or Browser is not 32 bit IE.");
   /*@end
@*/

By manipulating the comment tag,  the "else" part in this example will get picked up by all non IE browsers such as Firefox, plus non 32 bit IE as well. Study the comments until your head spins, and you'll see the logic. :)

if, elseif, else logic (IE exclusive)

Moving on, time for the full Monty:

/*@cc_on
   @if (@_jscript_version >= 5)
      document.write("IE Browser that supports JScript 5+");
   @elif (@_jscript_version >= 4)
      document.write("IE Browser that supports JScript 4+");
   @else
      document.write("Very old IE Browser");
   @end
@*/

if, elseif, else logic II (other browsers inclusive)

/*@cc_on
   /*@if (@_jscript_version >= 5)
      document.write("IE Browser that supports JScript 5+");
   @elif (@_jscript_version >= 4)
      document.write("IE Browser that supports JScript 4+");
   @else @*/
      document.write("Non IE Browser (one that doesn't support JScript)");
   /*@end
@*/

Sane deal here. In this 2nd example of the 2nd set, the final "else" statement gets picked up by non IE browsers.

 

文章来自:http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

分享到:
评论

相关推荐

    GCC编译错误类型

    建议在使用传统C编译选项时避免使用#elseif语句。在传统C中,#elseif可能被视为两个独立的预处理器指令,导致意外的行为。 35. **“Traditional C ignores #%s with the # indented”** 在传统C中,如果#号被...

    IF条件语句的翻译程序设计

    ### IF条件语句的翻译程序设计 #### 1、系统描述(问题域描述) ##### ①设计目的 本次课程设计旨在深入理解编译原理中的语法分析与语义分析技术,特别是针对IF条件语句的翻译。通过设计、实现一个简单的语法及...

    IAR编译错误信息表[参照].pdf

    20. Unmatched #else ،#endif或#elif(不匹配的 # else ,#endif 或#elif):致命错误,丢失了 #if ,#ifdef或#ifdef。 21. No such pre-processor command:’ name’(无这样的预处理命令):#后随未知的识别符。...

    Python基础语法合集大全.docx

    - `if`, `elif`, `else` —— 条件语句 - `for`, `while` —— 循环结构 - `import`, `from` —— 导入模块 - `try`, `except`, `finally` —— 异常处理 - `with` —— 用于上下文管理 - `yield` —— 用于生成器...

    python基础知识(实用)收集.docx

    - `elif` 和 `else` 子句可以添加到 `if` 语句中,以便处理更多的条件分支。 **循环语句**: - `for` 循环用于遍历序列(如列表、元组、字典、字符串等)中的元素。 - `while` 循环用于重复执行一段代码,直到给定...

    python、mongodb、redis.docx

    - **条件语句:** 使用 `if`、`elif` 和 `else` 结构来根据不同的条件执行不同的代码块。逻辑运算符 `and`、`or` 和 `not` 可用于组合条件。 - **循环结构:** `while` 循环基于条件重复执行一段代码,直到条件不再...

    python基础教程至60课(基础).docx

    - **条件语句**:`if` 语句用于基于不同的条件执行不同的代码块。 - **语法**: - `if condition:` - `# 代码块` - `elif another_condition:` - `# 代码块` - `else:` - `# 代码块` #### Python 第 8 课:...

    前端插件art-template模板引擎art-template-test示例代码

    它提供了丰富的控制结构,如条件语句(`#if`、`#else`、`#elif`)、循环语句(`#each`)以及表达式(`${}`)等,使得我们可以轻松地将数据嵌入到HTML模板中。例如: ```html #each items as item index ${...

    fis-parser-artc:fis 编译艺术模板的解析器

    4. **条件判断**:使用`#{if}`、`#{else}`和`#{elif}`进行条件判断,例如`#{if user.isAdmin}管理员#{/if}`。 5. **块助词**:`#{block}`和`#{end}`用于创建可复用的模板片段。 **fis-parser-artc的工作流程** 1. ...

    Python知识点(史上最全).pdf

    在选择结构方面,Python使用`if-elif-else`来实现条件判断,其中任何非空值都被视为True,空字符串、空元组、空列表和空字典以及0被视为False。字符串前加上`r`表示原始字符串,不会进行转义处理。列表是Python中...

    应届毕业生笔试题(软件)

    常用的条件编译指令包括 `#ifdef`, `#ifndef`, `#if`, `#elif`, `#else` 和 `#endif`。通过条件编译,可以在不同的编译环境下编译不同的代码,从而达到代码复用的目的。 - **示例代码**: ```c #ifdef DEBUG ...

    Python语法整理_CYR.docx

    - **不换行输出**:可以通过设置 `end` 参数为 `' '` 来实现不换行输出,例如:`print(x, end='')`。 #### 六、Python变量类型 - **变量赋值**:变量在赋值时创建。可以进行单变量赋值或多变量赋值。 - **数据类型...

    python基础语法全解

    1. **条件语句**:包括`if`、`elif`、`else`。 2. **循环语句**:包括`for`循环和`while`循环。 3. **循环中的`else`语句**:在循环正常结束时执行。 4. **`pass`语句**:空操作,起到占位的作用。 #### 十四、...

    Python基础语法合集

    - `if`, `elif`, `else`: 条件分支。 - `import`, `from`: 导入模块。 - `try`, `except`, `finally`: 异常处理。 - `with`, `as`: 上下文管理器。 - `global`, `nonlocal`: 定义变量的作用域。 #### 四、Python中...

    Lindenmayer-Systems

    turtle.end_fill() lindenmayer_system = LSystem('F', {'F': 'FF-[-F+F+]F+'}, 25, 5) turtle = turtle.Turtle() for _ in range(lindenmayer_system.iterations): lindenmayer_system.apply_rule() lindenmayer...

Global site tag (gtag.js) - Google Analytics