Skip to content Skip to sidebar Skip to footer

Javascript - Error When A Script Tag In A String

I have a page with the following code: '; This results in an error: Uncaught SyntaxError: Unexpected to

Solution 1:

the error went away when I escaped the / with a \.

http://jsbin.com/xugabolu/1/edit


Solution 2:

The hackish work-around for this is something like:

<script>
window.str = '<scr' + 'ipt></scri' + 'pt>';
</script>

In this way, you wind up with the string you want without confusing the parser.

Or, insert it as a DOM element if that is the ultimate goal.


Post a Comment for "Javascript - Error When A Script Tag In A String"