sammiwei
sammiwei

Reputation: 3200

Unexpected token ILLEGAL in javascript for array

var list =["<script></script>", "A", "B", "C"]

I got unexpected token ILLEGAL error here. Say, if I do want the script tag to be included, but just plain text, how can I format the list. Thanks!

Upvotes: 0

Views: 931

Answers (3)

Sushanth --
Sushanth --

Reputation: 55740

Need to escape the </script>" tag

<\/script>"

var list =["<script><\/script>", "A", "B", "C"];

Otherwise it tends to see it as the end of the script tag ..

Upvotes: 2

Quentin
Quentin

Reputation: 943210

If you are using an inline script1, then </script> will terminate the script element in the middle of the array constructor (all the HTML is parsed before the text nodes in the element are passed to the JS engine, </script> gets no special treatment for being inside a JS string literal).

Escape the /:

var list =["<script><\/script>", "A", "B", "C"]

You could also move the script to an external file and src it.

  1. i.e. a <script> element with the JS directly inside it as opposed to one with a src attribute or an intrinsic event attribute like onclick.

Upvotes: 6

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382112

Replace with

var list =["<script></"+"script>", "A", "B", "C"]

The "</script>" was ending the script element in which you have your script.

Upvotes: 3

Related Questions