Archive
Old post. Originally published 9 December 2013 on the WordPress version of this site.
Preserved here for the search engines and the curious. Old me had opinions.
JavaScript: Uncaught SyntaxError: Unexpected token ILLEGAL
So hopefully this will save you a shit load of time! I’ve been dealing with a mysterious JavaScript error over the past few days. It was a simple button that when clicked fired some JavaScript.
Uncaught SyntaxError: Unexpected token ILLEGAL
I saw articles talking about hidden characters, UTF16 encoding all kinds of stuff. Finally it clicked.
This is what I started with:
<a href="javascript:void(0)" id="<?php echo 'permission-' . $acoPath[count( $acoPath) -1]['Aco']['id'] . '-' . $data['id']; ?>" onclick="acos.toggle(<?php echo $this->Html->url( "/user_permissions/toggle"); ?>, <?php echo $acoPath[count( $acoPath) -1]['Aco']['id']; ?>, <?php echo $data['id']; ?>); return false;">
If you’re passing a string as a parameter, put it in single quotes! Simply doing this:
<a href="javascript:void(0)" id="<?php echo 'permission-' . $acoPath[count( $acoPath) -1]['Aco']['id'] . '-' . $data['id']; ?>" onclick="acos.toggle('<?php echo $this->Html->url( "/user_permissions/toggle"); ?>', '<?php echo $acoPath[count( $acoPath) -1]['Aco']['id']; ?>', '<?php echo $data['id']; ?>'); return false;">
Got rid of the error and left me free to have another cup of coffee and move on!
Filed under Developer. No comments, on purpose.