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:
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
分享到:
相关推荐
激活条件编译支持。 @cc_on 备注 @cc_on 语句激活脚本... 要求 版本 3 请参见 参考 @if…@elif…@else…@end 语句 @set 语句 代码如下: /*@cc_on @if (@_jscript_version > 5.7) [removed](“You are using IE8+”);
CMSIS目前支持三大主流的工具链,即ARM RealView (armcc), IAR... #if defined ( __CC_ARM ) #define __ASM __asm /*!*/ #define __INLINE __inline /*!*/ #elif defined ( __ICCARM__ ) #define __ASM __asm /*!*/
python基础_9_if_elif_else_(教学教程tutorial)
你学习了如何编写简单的if 语句、 if-else 语句和if-elif-else 结构。 在程序中, 你使用了这些结构来测试特定的条件, 以确定这些条件是否满足。 你学习了如何在利用高效的for 循环的同时, 以不同于其他元素的...
原来我想当然的认为“if…if…else…”是“if…elif…else…”的简化结构(这个错误的看法好像还是从学C语言继承过来的)。学了这么多天才发现其中的区别啊。下面先说说python,然后再说一下C语言里面的if语句。 ...
——学习参考资料:仅用于个人学习使用! 本代码仅作学习交流,切勿用于商业用途,否则后果自负。若涉及侵权,请联系,会尽快处理! 未进行详尽测试,请自行调试!
Python开发基础知识,if-elif-else语句综合应⽤,新⼿使⽤教程 Python 中条件选择语句的第三种形式:if-elif-else,对于前⾯介绍的 if 分⽀语句,执⾏过程⾮常简单,判断 if 语句中的表达式是否为 True真,如果为...
Python if-elif-else知识点 if age >= 18: print 'adult' else: if age >= 6: print 'teenager' else: print 'kid' 这样写出来,我们就得到了一个两层嵌套的 if … else … 语句。这个逻辑没有问题,但是,...
主要介绍了Python中的if、else、elif语句的用法讲解,条件判断语句是程序中流程控制的基础办法之一,需要的朋友可以参考下
elif [条件]: … else: … e.g.1 if it’s hot It’s a hot day Drink plenty of water otherwise if it’s cold It’s a cold day Wear warm clothes otherwise It’s a lovely day Solution...
PYTHON学习教程:条件判断(if、elif.、else代码练习print Run).docx
这次的自制编程语言项目实行了if-elif-else,可以是嵌套的,总之功能强大! 还做了个简易的Qt的信号槽,可以实现全局信号->非成员函数,全局信号->成员函数!
Webpack加载器,可直接从Webpack进行JavaScript或TypeScript条件编译( #if ... #elif ... #else ... #endif )。 有条件的编译指令写在///三斜杠注释中,这样它们就不会影响普通JavaScript或TypeScript解析。 ...
像if-else链一样结构,第一个匹配的分支是发出的项目。 [ dependencies ] cfg-if = " 1.0 " 例子 cfg_if :: cfg_if! { if #[cfg(unix)] { fn foo () { /* unix specific functionality */ } } else if #[cfg...
# 选择的类型来定义字符集合 if password_type == "1": charset = lowercase # 小写字母,则字符集合为小写字母 password_type = "小写字母" elif password_type == "2": charset = uppercase # 大写字母,则字符...
ubuntu18.04版本以上系统编译君正软件包问题解决方案及相应软件包。 在ubuntu23.04版本系统中验证通过。 主要解决M4,fakeroot,以及autocnf软件版本升级。 fakeroot软件编译报出的‘_STAT_VER’ undeclared 错误,...
python中使⽤ if...elif...else 语句结构,⽽在robotframework中如下: run keyword if 判断条件 其他关键字 ... ELSE IF 判断条件 其他关键字 ... ELSE 其他关键字 注意:ELSE IF, ELSE⼀定要全⼤写! ⼆ ⼆. 使⽤...
执行语句2 else: 执行语句3 ``` 在if-elif-else语法中,程序会先判断条件1是否为True,如果是,则执行语句1,否则继续判断条件2,如果条件2为True,则执行语句2,否则执行语句3。 嵌套if语法可以让程序根据多个条件...