0
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!

sammiwei
  • 3,140
  • 9
  • 41
  • 53

3 Answers3

6

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.
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I am trying to use this one right now: var list = ["<script></script>", "A", "B", "C"], it gives me an error of "unexpected token &". Any further explanation why this does not work? Thanks! – sammiwei Nov 12 '12 at 21:28
3

Replace with

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

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

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Splitting the string into two parts and concatenating them is marginally less efficient and a fair bit less readable then just escaping the `/`. – Quentin Nov 12 '12 at 18:54
2

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 ..

Sushanth --
  • 55,259
  • 9
  • 66
  • 105