免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5100 | 回复: 2

html - javascript注释中的if else [复制链接]

论坛徽章:
0
发表于 2009-06-04 16:01 |显示全部楼层
http://www.seo-space.net/blog/41 ... al-Compilation.html

在js的注释中是可以有流程控制,这个也是在今天才发现。虽然这个东西用处不大,但是挺好玩的。


  1. /*@cc_on
  2.   @if (@_win32)
  3.     document.write("OS is 32-bit. Browser is IE.");
  4.   @else
  5.     document.write("OS is NOT 32-bit. Browser is IE.");
  6.   @end
  7. @*/
复制代码


上面的代码就是注释中的流程控制,用@cc_on来开头就可以了,在里面就可以写if...else...end了

if语句有以下四个:
@if
@elif
@else
@end

Variables有:
@_win32 如果是win32就返回真,否则返回NaN.
@_win16 如果是win16就返回真,否则返回NaN.
@_mac 如果是apple就返回真,否则返回NaN.
@_alpha 如果运行在“DEC Alpha processo”上就返回真,否则返回NaN.
@_x86 如果是x86就返回真,否则返回NaN.
@_mc680x0 如果是Motorola 680x0 cpu就返回真,否则返回NaN.       
@_PowerPC 如果是PowerPC cpu就返回真,否则返回NaN.
@_jscript 总是返回真
@_jscript_build The build number of the JScript scripting engine.
@_jscript_version 以x.x型式返回本号

IE4 supports JScript 3.x
IE5.x supports JScript 5.5 or less
IE6 supports JScript 5.6

The version number reported for JScript .NET is 7.x.
@_debug Returns true if compiled in debug mode, otherwise false.
@_fast Returns true if compiled in fast mode, otherwise false.


如果你想自己defined Variables可以使用@set
请看defined Variables例子:
@set @myvar1 = 35
@set @myvar3 = @_jscript_version

运算符:
! ~
* / %
+ -
<< >> >>>
< <= > >=
== != === !==
& ^ |
&& |

下面为try catch的例子:


  1. function HttpRequest(url, parameters){
  2. var pageRequest = false //variable to hold ajax object
  3. /*@cc_on
  4.   @if (@_jscript_version >= 5)
  5.     try {
  6.     pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
  7.     }
  8.   catch (e){
  9.     try {
  10.     pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
  11.     }
  12.     catch (e2){
  13.     pageRequest = false
  14.     }
  15.   }
  16. @end
  17. @*/

  18. if (!pageRequest && typeof XMLHttpRequest != 'undefined')
  19. pageRequest = new XMLHttpRequest()
  20. }
复制代码

论坛徽章:
0
发表于 2009-06-08 17:07 |显示全部楼层
学习

论坛徽章:
0
发表于 2009-07-02 14:43 |显示全部楼层

自顶
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP