Search code examples
javascriptinternet-explorer-10

IE10 throwing javascript error (SCRIPT1004)


I'm noticing a strange behaviour with IE10.

I have a javascript file with this content:

var x = 1;

//@deprecated, use static version
var y=function(interval){
    console.log(interval);
};

The file is encoded with UTF-8.

I have an html file with meta tag

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

If the previous file is included via <script> tag, it works.

If I instead download the file creating a script element via javascript and appending the script in the head:

var script = document.createElement('script'),
head = document.getElementsByTagName('head')[0];

script.type= 'application/javascript';
script.src = ''; // omissis

head.appendChild(script);

It works for IE11 but not for IE10: it is throwing error

(SCRIPT1004): expected ';'

The strange thing is: the line and column number of the error are pointing to the comma after the //@deprecated: but this should be a comment, it should not throw any error.

BTW: the javascript that creates the script tag and append it to the head is working for all javascript files and for all browser. It seems to be broken only if inside the file there is the //@deprecated comment.

Thank you, cheers


Solution

  • Ok, thanks to @Deepak-MSFT I found the error.

    The error was caused by another script that was loaded before the execution of the snippet.

    In this script, there was the code /@cc_on!@/ that activates the conditional compilation flag for IE(< 11) browser. Here is another stack overflow question related to this:

    what does this comment /*@cc_on!@*/0 do inside an if statement in javascript?

    If I comment out the line, the problem disappear.

    Thanks to @Deepak-MSFT, because he tried to reproduce the problem by doing the steps indicated by me - this was my fault, because I descripted the minimal steps to reproduce the problem without actually doing a test by myself.