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!

2
  • What exactly doesn't work? That should execute just fine in a browser. Commented Nov 12, 2012 at 18:51
  • The browser is mangling it. Break the "script" string up into two concact peices. Commented Nov 12, 2012 at 18:53

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to use this one right now: var list = [&quot;&lt;script&gt;&lt;&#47;script&gt;&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;], it gives me an error of "unexpected token &". Any further explanation why this does not work? Thanks!
3

Replace with

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

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

1 Comment

Splitting the string into two parts and concatenating them is marginally less efficient and a fair bit less readable then just escaping the /.
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 ..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.