Home  >  Article  >  Web Front-end  >  JScript的条件编译_javascript技巧

JScript的条件编译_javascript技巧

WBOY
WBOYOriginal
2016-05-16 19:13:061087browse

微软对条件编译的解释是:
条件编译的某些典型用途包括使用 Jscript 中的新特性、在脚本中嵌入调试支持以及跟踪代码的运行。

先看一段代码简单的了解下:


var s=0//@cc_on+1
alert(s)


如果你使用的是IE可以看到,浏览器弹出的提示s的值是"1";

var s=0//@cc_on+1
在IE中等同于:

var s=0+1

条件编译为在注释中运行代码提供了途径。

一般我们在编写JavaScript代码时很少用到条件编译。不过条件编译由于其本身的特性,对我们检测系统,自动识别浏览器编程却提供了方便。
有一段很经典的代码:
var isMSIE = /*@cc_on!@*/false;

也可以这样:
var IE=0//@cc_on+1

更复杂点的:

var IEVersion=/*@cc_on function(){ switch(@_jscript_version){ case 1.0:return 3; case 3.0:return 4; case 5.0:return 5; case 5.1:return 5; case 5.5:return 5.5; case 5.6:return 6; case 5.7:return 7; }}()||@*/0;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn