IE的那些@cc_on

最近在编写一个获取DOM元素在文档中的X/Y坐标的时候,遇到了一个ie6,ie7下多出2个像素的问题,临时想到了ie的那套@cc_on特定判断语法,网上狂搜关于这方面的代码,搜出来一大把。现在将它们总结整理如下:

@_jscript_version是IE的版本号,/*@cc_on …… @*/就是IE专属的一个判断执行块,@_x86和@_x32 表示OS的位数。

 /*@cc_on
     @if (@_jscript_version < 5.7) {
         //code for IE6 and lower
     } @else {
        //code for IE7 and upper
     }
    @end
@*/

////////////////////////////////////////////

/*@cc_on
    @if (@_jscript_version > 5.7)
         document.write("You are using IE8+");

    @elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
         document.write("You are using IE7");

    if (@_jscript_version==5.6 ||(@_jscript_version==5.7 && navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1))
         document.write("You are using IE6");

    @elif (@_jscript_version == 5.5)
    document.write("You are using IE5.5");

    @else
    document.write("You are using IE5 or older");
    @end
@*/

//////////////////////////////////////////////////////////

/*@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
@*/

通过上面的@cc_on,你可以针对目前IE的各版本来编写js脚本,但是还是那句话:可以不用的就别用吧,它也确实是有些丑陋。:)