check out my new blog at gmarik.info/blog

Saturday, November 18, 2006

eval.call(window, this.text) syntax error

I was playing with jQuery and its jQuery Form plugin.
Everythig went fine and i got AHAH calculator working until i tried to test IE...
And yes, it didn't work...Each time a got unknown error.
So I took a debugger and started fixing...
Eventually I found the place causing error

...
call.apply(window, this.text);
...

which is to evaluate scripts from received HTML response.
But what actually caused the error was that each script was commented out like:

<script type="text/javascript">
<!--
alert('Im commented')
-->
</script>

and MS IE eval function implementation isn't capable evaluating that.
So

eval("<!-- alert('Im commented') -->"); /** Causes Syntax error in IE (6 tested)*/

while

eval("alert('Im commented')"); /** Works fine*/


Yeah, that worth to know...

Seems that Firefox behaves badly as well evaluating commented javascript, it just goes crazy without letting you know about...:)

UPDATE: well, the solution to this stuff is to comment javascript HTML comments .So eval() will not see HTML-comments:

<script> // <!--
...
//--></script>