开发者

Reason behind a JavaScript parsing error in MSIE 8

Given something like

var obj = {
     foo: function(){
         try{
             doSomething();
          }catch(ex){
               //@TODO - report error
          }
     }
 }

MSIE 8 would thr开发者_如何学运维ow up a "Missing semi-colon on line #" which was where the @TODO was.

After I sed'd the dozens of @TODO's to be !TODO, MSIE was able to properly parse the script and life went on. Am I missing something here, does MSIE use some sort of non-standard mechanism like //@PRAGMA ?

Googling for @TODO or //@ didn't bring up anything useful.


This is to do with conditional compilation, an IE-only invention for varying JScript (IE's name for their flavour of ECMAScript) compilation based on information about the browser and environment. The syntax involves the @ symbol followed by a string to make up a variable, directive or statement. In this case, the presence of @TODO directly after the start of a comment is causing the comment text to be interpreted as a conditional compilation statement, with @TODO being a conditional compilation variable (with a value of NaN: see http://msdn.microsoft.com/en-us/library/k0h7dyd7%28v=VS.80%29.aspx).

Conditional compilation statements are generally contained within JavaScript comments: these are there to prevent other browsers from attempting to interpret the code but are not in fact required to trigger conditional compilation. The MSDN documentation is here:

http://msdn.microsoft.com/en-us/library/ahx1z4fs%28v=VS.80%29.aspx

This feature is only enabled for code that appears after conditional compilation is enabled, which is achieved with

/*@cc_on @*/

Therefore if you can find this line and remove it then your //@TODO - report error will be fine as it is. However, some of your code may rely on conditional compilation so this may not be an option. A workaround is to insert a space between the start of the comment (either // or /*) and the @ symbol:

// @TODO - report error

Microsoft's documentation is not clear enough to know why this works, since conditional compilation variables also work outside comments:

// The following works in IE:

/*@cc_on @*/
var j = @_jscript_build;
alert(j);

Therefore the safest option would be to avoid use of @TODO altogether.


The comment+@ syntax is used for conditionnal compilation in Internet Explorer. See http://www.javascriptkit.com/javatutors/conditionalcompile.shtml


I remembered seeing a post like this on our forums, seems it gets interpreted by JScript:

http://www.sencha.com/forum/showthread.php?92186-FIXED-579-Comment-line-leads-to-IE7-error&highlight=comment

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜