Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 throw 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.

like image 785
David Avatar asked Jun 22 '10 09:06

David


2 Answers

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.

like image 150
Tim Down Avatar answered Nov 13 '22 05:11

Tim Down


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

like image 37
slaphappy Avatar answered Nov 13 '22 04:11

slaphappy